您的位置:首页 > 产品设计 > UI/UE

iOS-UITableView使用详解深入浅出

2015-03-29 16:58 246 查看
iOS-UITableView使用详解深入浅出

首先,我们来分析一下UITableView。

UITableView是一个显示多项的视图,是一个视图列表,通过dataSource我们可以得到要显示的信息,通过delegate我们可以把对UITableView的操作传递给控制器,dataSource就是一个提供数据的人,delegate就是对用户的操作进行处理,dataSource和delegate可以是同一个对象也可以是不同的对象。。UITableviewCell是单个项的载体。

但一个比较拟人的比方。把对UITableView的操作进行分解。

首先,人的操作我们可以看作是一个提问的人。

UITableView是一个什么都不知道的大傻子,它什么事情都要向dataSource,和delegate询问,dataSource和delegate是垂帘听政,UITableView就是傀儡皇帝。UITableView负责把dataSource和delegate的信息传递给用户。

有人就问,小皇帝,后宫有多少太监总管,UITableView不知道啊,它就通过dataSource去询问,于是dataSource就返回一个数字告诉人们有多少个。

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
人们又问这些太监总管各自管理多少个太监,小皇帝还是不知道,它就通过dataSource去询问,dataSource就通过对应的太监总管的名字进行钦点并一一返回。

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
人们有说叫他们出来看看,也就是UITableView显示Cell,问题来了假设太监都没有穿衣服,现在只有十件衣服能穿,但是太监有100个。不可能叫他们光着身子去见人,于是叫到哪个小太监就让那个小太监穿着衣服出去,当小太监完成任务之后它就把衣服脱了让给需要的人。太监衣服就是cell要见人的东西,小太监就是数据源。太监衣服的从用就是UITableViewCell的重用。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
。询问是否移动
。询问操作类型(增加删除编辑)
。询问被点击了怎么办



所以在编程时要设计delegate和dataSource的数据源和UI统一带有indexPath参数的当改indexPath发生变化都会调用的方法,你要根据indexPath来设计自己对每个cell操作之后的反应和对应indexPath的数据源的同步。

UITableView的编程无非就是delegate和dataSource的设计而已。显示功能就交给UITableView.

简单使用,其他代理方法可以自己根据API或者头文件进行试验编程。

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end


@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation ViewController{

NSMutableArray *_dataArr;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

_tableView.dataSource = self;
_tableView.delegate = self;

_dataArr = [[NSMutableArray alloc]initWithObjects:@"item1",@"item2",@"item3",@"item4",@"item5",@"item6", nil];
_tableView.editing = YES;

}

//返回有多少组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
//返回对应组中有多少个项
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _dataArr.count;
}

//UITableView在渲染自己的时候通过循环调用改方法来得到UITableViewCell,并把TableViewCell贴到UITableView中。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell;
static NSString *cellIdentifier = @"cellIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

[self configTableViewCell:cell withIndexPath:indexPath];

return cell;
}

//配置UITableViewCell的显示内容,通过读取数据源
-(void)configTableViewCell:(UITableViewCell *)cell withIndexPath:(NSIndexPath *)indexPath{
cell.textLabel.text = _dataArr[indexPath.row];
}

@end


抽象图:



小结:
上面纯粹个人扯淡,只要了解功能类MVC设计模式的划分设计,结合对象内存模型,模拟成生活中的事情,把对象模拟成生活中的人或者事,计算机世界就是人的世界,
UITableView的操作和数据源请求都是通过delegate和dataSource询问的。它就是一个没有大脑的UI显示工具。什么事都是delegate决定,dataSource提供资源。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: