iOS 解决cell因重用机制出错的问题

来源:互联网 发布:js获取上传文件的md5 编辑:程序博客网 时间:2024/06/03 16:01

1 不用重用

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  {            NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", [indexPath section], [indexPath row]];//以indexPath来唯一确定cell      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell      if (cell == nil) {          cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];      }      //...其他代码  } 

2 删除重用cell的子视图

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  {      static NSString *CellIdentifier = @"Cell";      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell      if (cell == nil) {          cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];      }      else      {          //删除cell的所有子视图          while ([cell.contentView.subviews lastObject] != nil)          {              [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];          }      }      //...其他代码  }

转载的链接  http://blog.csdn.net/iqrocket/article/details/8812882


0 0