美文网首页
IOS tableViewCell 复用问题

IOS tableViewCell 复用问题

作者: Shui_灰灰 | 来源:发表于2016-06-22 11:59 被阅读1186次

    // 方案一  通过不让他重用cell 来解决重复显示

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

    {

    // 定义唯一标识

    static NSString *CellIdentifier = @"Cell";

    // 通过indexPath创建cell实例 每一个cell都是单独的

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    // 判断为空进行初始化  --(当拉动页面显示超过主页面内容的时候就会重用之前的cell,而不会再次初始化)

    if (!cell) {

    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    }

    cell.textLabel.text = @"text";

    return cell;

    }

    // 方案二  同样通过不让他重用cell 来解决重复显示 不同的是每个cell对应一个标识

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

    {

    // 定义cell标识  每个cell对应一个自己的标识

    NSString *CellIdentifier = [NSString stringWithFormat:@"cell%ld%ld",indexPath.section,indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // 判断为空进行初始化  --(当拉动页面显示超过主页面内容的时候就会重用之前的cell,而不会再次初始化)

    if (!cell) {

    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    }

    cell.textLabel.text = @"text";

    return cell;

    }

    // 方案三  当页面拉动需要显示新数据的时候,把最后一个cell进行删除 就有可以自定义cell 此方案即可避免重复显示,又重用了cell相对内存管理来说是最好的方案 前两者相对比较消耗内存

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

    {

    // 定义唯一标识

    static NSString *CellIdentifier = @"Cell";

    // 通过唯一标识创建cell实例

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // 判断为空进行初始化  --(当拉动页面显示超过主页面内容的时候就会重用之前的cell,而不会再次初始化)

    if (!cell) {

    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    }

    else//当页面拉动的时候 当cell存在并且最后一个存在 把它进行删除就出来一个独特的cell我们在进行数据配置即可避免

    {

    while ([cell.contentView.subviews lastObject] != nil) {

    [(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview];

    }

    }

    cell.textLabel.text = @"text";

    return cell;

    }

    相关文章

      网友评论

          本文标题:IOS tableViewCell 复用问题

          本文链接:https://www.haomeiwen.com/subject/qqxndttx.html