IOS中TableView的用法

一、UITableView

1.数据展示的条件

1> UITableView的所有数据都是由数据源(dataSource)提供的,所以要想在UITableView展示数据,必须设置UITableView的dataSource数据源对象

2> 要想当UITableView的dataSource对象,必须遵守UITableViewDataSource协议,实现相应的数据源方法

3> 当UITableView想要展示数据的时候,就会给数据源发送消息(调用数据源方法),UITableView会根据方法返回值决定展示怎样的数据

 

2.数据展示的过程

1> 先调用数据源的

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

得知一共有多少组

 

2> 然后调用数据源的

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

得知第section组一共有多少行

 

3> 然后调用数据源的

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

得知第indexPath.section组 第indexPath.row 行显示怎样的cell(显示什么内容)

 

3.常见数据源方法

1> 一共有多少组

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

 

2> 第section组一共有多少行

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

 

3> 第indexPath.section组 第indexPath.row行显示怎样的cell(显示什么内容)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

 

4> 第section组显示怎样的头部标题

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;

 

5> 第section组显示怎样的尾部标题

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;

 

4.tableView刷新数据的方式

1> 修改模型数据

 

2> 刷新表格

* reloadData 整体刷新(每一行都会刷新)

* - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

局部刷新

//局部section刷新
    NSIndexSet * nd=[[NSIndexSet alloc]initWithIndex:1];//刷新第二个section
    [tview reloadSections:nd withRowAnimation:UITableViewRowAnimationAutomatic];
    //局部cell刷新  www.2cto.com
    NSIndexPath *te=[NSIndexPath indexPathForRow:2 inSection:0];//刷新第一个section的第二行
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:te,nil] withRowAnimation:UITableViewRowAnimationMiddle];

 刷新动画:

typedef NS_ENUM(NSInteger, UITableViewRowAnimation) {
UITableViewRowAnimationFade,   //淡入淡出
UITableViewRowAnimationRight,  //从右滑入         // slide in from right (or out to right)
UITableViewRowAnimationLeft,   //从左滑入
UITableViewRowAnimationTop,     //从上滑入
UITableViewRowAnimationBottom,  //从下滑入
UITableViewRowAnimationNone,            // available in iOS 3.0
UITableViewRowAnimationMiddle,          // available in iOS 3.2.  attempts to keep cell centered in the space it will/did occupy
UITableViewRowAnimationAutomatic = 100  // available in iOS 5.0.  chooses an appropriate animation style for you
};

 

5.性能优化

1> 定义一个循环利用标识

static NSString *ID = @"C1";

 

2> 从缓存池中取出可循环利用的cell

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

 

3> 如果缓存池中没有可循环利用的cell

if (cell == nil) {

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];

}

 

4> 覆盖cell上面的数据

cell.textLabel.text = [NSString stringWithFormat:@"第%d行数据", indexPath.row];

转载于:https://www.cnblogs.com/endtel/p/5368499.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值