iOS UIKit - UITableView

一、UITableView使用步骤

实现UITableViewDataSource协议里的方法

1.有多少块数据

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;//整体显示1块数据
    }

2.每块数据有多少行

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return 6;//每块数据显示6行
    }

3.每行显示什么

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell  = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        if (indexPath.section==0)//indexPath相当于树
        {
            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";//添加标记,复用池cell类型很多,防止混淆
        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
            cell.textLabel.text = name;

            return cell;
}

三、UITableView性能优化

(1)重用

重用cell/header/footer

     在UITableView的dataSource中实现的tableView:cellForRowAtIndexPath:方法,需要为每个cell调用一次,它应该快速执行。所以你需要尽可能快地返回重用cell实例,不要在这里去执行数据绑定,因为目前在屏幕上还没有cell。为了执行数据绑定,可以在UITableView的delegate方法tableView:willDisplayCell:forRowAtIndexPath:中进行。这个方法在显示cell之前会被调用。

(2)渲染

减少透明图层

(3)异步

多线程

(4)缓存

缓存

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值