一、UITableView使用步骤
实现UITableViewDataSource协议里的方法
1.有多少块数据
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
2.每块数据有多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 6;
}
3.每行显示什么
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
if (indexPath.section==0)
{
if (indexPath.row==0)
{
cell.textLabel.text=@"第一区域第一行";
}
else if (indexPath.row ==1 )
{
cell.textLabel.text=@"第一区域第二行";
}
}
else if (indexPath.section ==1){
if (indexPath.row==0)
{
cell.textLabel.text=@"第二区域第一行";
}
else if (indexPath.row ==1 )
{
cell.textLabel.text=@"第二区域第二行";
}
}
return cell;
}
若要控制UITableView具体显示方式和大小,需要实现UITableViewDelegate协议里的方法
二、UITableView使用说明
1.多少块数据和每块是多少行数据,一般是通过加载plist,转换成模型。
2.组是section,行是row,具体每一行是UITableViewCell来显示的。
设置cell的步骤:
(1)创建一个cell(是否选择复用)
(2)为cell设置数据
(3)返回cell
设置cell的样式
(1)系统提供了四种样式

(2)通过xib自定义cell,适用于样式一致,如团购
(3)通过纯代码定义
cell的复用原理
界面显示2个UITableViewCell时,只需要3个cell,2个用于显示,还有1个在复用池等待,滑动到第一个出去时,从复用池找下一个替上,如此循环节约内存。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
InfoModel *infoCell = self.infoGroups[indexPath.section];
NSString *name = infoCell.infos[indexPath.row];
cell.textLabel.text = name;
return cell;
}
三、UITableView性能优化
(1)重用
在UITableView的dataSource中实现的tableView:cellForRowAtIndexPath:方法,需要为每个cell调用一次,它应该快速执行。所以你需要尽可能快地返回重用cell实例,不要在这里去执行数据绑定,因为目前在屏幕上还没有cell。为了执行数据绑定,可以在UITableView的delegate方法tableView:willDisplayCell:forRowAtIndexPath:中进行。这个方法在显示cell之前会被调用。
(2)渲染
减少透明图层
(3)异步
多线程
(4)缓存
缓存