iphone开发之表格组件UITableView的使用(四) 通过加载plist文件展示单组数据
2015-08-31 19:43
846 查看
1、单元格样式UITableViewCellStyleDefault 即默认样式是不显示小的Label简介的即不显示detailTextLabel上的Text的。
UITableViewCellStyleSubtitle样式:都显示,并且detailTextLabel上的Text作为小字体简介放在textLabel大字体标签下边。
UITableViewCellStyleValue1样式:都显示。但是detailTextLabel上的Text放在textLabel的右面。
UITableViewCellStyleValue2样式:不显示图片框。
2、去掉状态栏的方法:添加以下方法。
-(BOOL)prefersStatusBarHidden
{
return YES;
}
3、如果想在单元格右边显示一个组件,方法如下:
(1)系统提供的的控件,如箭头,详细信息按钮,等可以用UITableViewCell类的accessoryType属性进行设置。例如:
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; // 添加箭头
(2)可以自定义单元格右边的accessory。如:
cell.accessoryView = [[UISwitch alloc] init]; // 添加按钮
4、统一设置UITableView的行高。
1>(在每一行行高都相同的情况下)
在viewDidLoad方法中添加以下语句即可:
self.tableView.rowHeight = 60;
2> 对于每行行高都不一样的情况,无法通过tableView.rowHeight来实现,此时只能通过一个代理方法实现。具体如下:
先让当前控制器遵守UITableViewDelegate代理协议。然后添加方法如下即可:
-(CGFloat)tableView :(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath
{
int rowNum = indexPath.row;
if(rowNum %2 ==0){
return 60;
}else{
return 100;
}
}
代码验证:
(1)(新建或加载plist文件)
(2)打开plist文件,查看对象字典属性。
(3)根据字典的各个键与对应值的类型新建model类
(4)为控制器添加数组属性,并重写数组属性的get方法实现plist文件的懒加载。
(5)添加UITableView组件属性,因为默认就是单组风格。不用特地设置。在viewDidLoad方法中进行属性设置。
(6)让控制器类遵守UITableViewDataSource数据源协议和UItableViewDelegate代理 协议,并设置自身的代理对象与数据源对象为当前控制器对象。即self.tableView.delegate = self;
self.tableView.dataSource=self;
(7)添加数据源协议中的方法
、、、、
plist文件如下:
将相应图片资源拉进工程中一个自设的文件夹,在Supporting files下新建model类如下:
参照文件中字典的属性,编辑Hero.h如下:
UITableViewCellStyleSubtitle样式:都显示,并且detailTextLabel上的Text作为小字体简介放在textLabel大字体标签下边。
UITableViewCellStyleValue1样式:都显示。但是detailTextLabel上的Text放在textLabel的右面。
UITableViewCellStyleValue2样式:不显示图片框。
2、去掉状态栏的方法:添加以下方法。
-(BOOL)prefersStatusBarHidden
{
return YES;
}
3、如果想在单元格右边显示一个组件,方法如下:
(1)系统提供的的控件,如箭头,详细信息按钮,等可以用UITableViewCell类的accessoryType属性进行设置。例如:
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; // 添加箭头
(2)可以自定义单元格右边的accessory。如:
cell.accessoryView = [[UISwitch alloc] init]; // 添加按钮
4、统一设置UITableView的行高。
1>(在每一行行高都相同的情况下)
在viewDidLoad方法中添加以下语句即可:
self.tableView.rowHeight = 60;
2> 对于每行行高都不一样的情况,无法通过tableView.rowHeight来实现,此时只能通过一个代理方法实现。具体如下:
先让当前控制器遵守UITableViewDelegate代理协议。然后添加方法如下即可:
-(CGFloat)tableView :(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath
{
int rowNum = indexPath.row;
if(rowNum %2 ==0){
return 60;
}else{
return 100;
}
}
代码验证:
(1)(新建或加载plist文件)
(2)打开plist文件,查看对象字典属性。
(3)根据字典的各个键与对应值的类型新建model类
(4)为控制器添加数组属性,并重写数组属性的get方法实现plist文件的懒加载。
(5)添加UITableView组件属性,因为默认就是单组风格。不用特地设置。在viewDidLoad方法中进行属性设置。
(6)让控制器类遵守UITableViewDataSource数据源协议和UItableViewDelegate代理 协议,并设置自身的代理对象与数据源对象为当前控制器对象。即self.tableView.delegate = self;
self.tableView.dataSource=self;
(7)添加数据源协议中的方法
、、、、
plist文件如下:
将相应图片资源拉进工程中一个自设的文件夹,在Supporting files下新建model类如下:
参照文件中字典的属性,编辑Hero.h如下:
// // Hero.h // LOL英雄榜UITableView加载plist展示单组数据 // // Created by apple on 15/8/31. // Copyright (c) 2015年 LiuXun. All rights reserved. // #import <Foundation/Foundation.h> @interface Hero : NSObject @property (nonatomic, strong) NSString *icon; @property (nonatomic, strong) NSString *intro; @property (nonatomic, strong) NSString *name; -(instancetype)initWithDict:(NSDictionary *)dict; +(instancetype)heroWithDict:(NSDictionary *)dict; @end编辑Hero.m如下:
// // Hero.m // LOL英雄榜UITableView加载plist展示单组数据 // // Created by apple on 15/8/31. // Copyright (c) 2015年 LiuXun. All rights reserved. // #import "Hero.h" @implementation Hero -(instancetype)initWithDict:(NSDictionary *)dict { if (self = [super init]) { // self.name = dict[@"name"]; // self.icon = dict[@"icon"]; // self.intro = dict[@"intro"]; [self setValuesForKeysWithDictionary:dict]; // KVC } return self; } +(instancetype)heroWithDict:(NSDictionary *)dict { return [[self alloc] initWithDict:dict]; } @end编辑控制器的.h文件如下:
// // ViewController.h // LOL英雄榜UITableView加载plist展示单组数据 // // Created by apple on 15/8/31. // Copyright (c) 2015年 LiuXun. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate> @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) NSArray *array; @end编辑控制器的.m文件如下:
// // ViewController.m // LOL英雄榜UITableView加载plist展示单组数据 // // Created by apple on 15/8/31. // Copyright (c) 2015年 LiuXun. All rights reserved. // #import "ViewController.h" #import "Hero.h" #define WIDTH [UIScreen mainScreen].bounds.size.width #define HEIGHT [UIScreen mainScreen].bounds.size.height @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 添加UITableView控件 self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain]; self.tableView.dataSource = self; self.tableView.showsVerticalScrollIndicator = NO; // 取消竖直滑动条 self.tableView.rowHeight = 70; // 只对于所有行高度都相同的情况下 [self.view addSubview:self.tableView]; } // 重写数组的get方法实现懒加载 -(NSArray *)array { if (_array == nil) { //查找plist文件的路径 NSString *path = [[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil]; // 根据路径读取plist文件到一个数组 NSArray *arrayDict = [NSArray arrayWithContentsOfFile:path]; // 新建空的一个专门存放model对象的可变数组 NSMutableArray *arrayModel = [NSMutableArray array]; // 遍历从文件读取存放所有字典的数组,逐个转成model对象存放到可变数组中 for (NSDictionary * dict in arrayDict){ // 新建一个model对象 Hero *hero = [Hero heroWithDict:dict]; [arrayModel addObject:hero]; } _array = arrayModel; } return _array; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } // 取消状态栏 -(BOOL)prefersStatusBarHidden { return YES; } // 利用UITableViewDelegate代理协议中的方法,即使行高不相同的也行 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row %2 == 0) { return 60; }else{ return 80; } } -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { return 1; } -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.array.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 新建指定格式的单元格对象 UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil]; // 获取model数据 Hero *hero = self.array[indexPath.row]; cell.imageView.image = [UIImage imageNamed:hero.icon]; cell.textLabel.text = hero.name; cell.detailTextLabel.text = hero.intro; // 添加箭头 /* accessoryType 的枚举值如下: UITableViewCellAccessoryNone, // don't show any accessory view UITableViewCellAccessoryDisclosureIndicator, // regular chevron. doesn't track UITableViewCellAccessoryDetailDisclosureButton, // info button w/ chevron. tracks UITableViewCellAccessoryCheckmark, // checkmark. doesn't track UITableViewCellAccessoryDetailButton NS_ENUM_AVAILABLE_IOS(7_0) // info button. tracks*/ if(indexPath.row <1){ cell.accessoryType = UITableViewCellAccessoryNone; // 默认状态下不显示任何组件 }else if(indexPath.row<2){ cell.accessoryType = UITableViewCellAccessoryDetailButton ; //显示详细信息按钮 }else if (indexPath.row<3){ cell.accessoryType = UITableViewCellAccessoryCheckmark; // 添加选择按钮 }else if (indexPath.row <4){ cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; // 添加箭头按钮 }else if (indexPath .row<5){ cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; // 同时添加箭头与详细信息的按钮 }else if (indexPath.row <6 ){ cell.accessoryView = [[UISwitch alloc] init]; }else cell.accessoryView = [[UIButton alloc] init]; return cell; } @end运行结果如下:
相关文章推荐
- 深入浅出CChart 每日一课——快乐高四第十四课 枝头红杏,Win32标准控件ChartCtrl之围城
- UIScrollView、UIPageControl
- easyui validatebox 去除校验 绑定校验
- UIScrollView+UIPageControl+NSTimer 图片自动滚动的两种实现方法
- UITabBarController的使用
- iOS7 CookBook精彩瞬间(三)UIActivityViewController的基本使用及自定义Activity
- iOS7 CookBook精彩瞬间(三)UIActivityViewController的基本使用及自定义Activity
- UIslide
- UIControl 及其子类
- Memo Mirantis Fuel 6.0安装札记
- [底层] 为什么Integer.MIN_VALUE-1会等于Integer.MAX_VALUE
- UITableview reloadData偶尔出现崩溃
- NGUI消耗性能分析
- 玩转EasyUI之ValidateBox
- iOS开发基础-UIButton
- iOS开发基础-UILabel
- openstack quick start
- 将ueditor上传的视频转换为flowplayer播放,并隐藏其播放地址
- Arduino板的DHT11温湿度传感器使用
- 工作3年。对于目前的工作环境不是很满意打算转战UI设计