UITableViewCell重用

UITableViewCell重用的第一种情况:


//自定义cell类名
NSString * const cellIdentifier = @"InterestingLabelCell";
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

      InterestingLabelCell *cell = [tableView  dequeueReusableCellWithIdentifier:cellIdentifier];

      //当cell为空的时候执行

      if(cell == nil){

          cell = [[[NSBundle mainBundle] loadNibNamed:cellIdentifier owner:self options:nil] lastObject];
      }
}

注意:(如果是使用nib构建UITableViewCell)方法很容易忽略的地方是:xib文件中对应的UITableViewCell中的identifier设置成对应的。例如该编码就是:InterestingLabelCell。不然会出现空数据的情况喔。


  

UITableViewCell重用的第二种情况:


//自定义cell类名

NSString * const cellIdentifier = @"InterestingLabelCell";

//用UiTableView控件注册一个Nib

- (void)viewDidLoad {

          [UITableView registerNib:[UINib nibWithNibName:cellIdentifier bundle:nil] forCellReuseIdentifier:cellIdentifier];

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
          InterestingLabelCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
}


猜你喜欢

转载自blog.csdn.net/Harvey_DHui/article/details/79501149