UITableview cell上的数据或控件覆盖问题

来源:互联网 发布:如何投资 知乎 编辑:程序博客网 时间:2024/05/16 05:12

当我们有时候用tableview写控件时,经常出现上下拖动后控件在一个cell上重复了,为此在重用时需要把其子视图先删除或归零

static NSString *identifier = @"identity";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
//cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:identifier];
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }else{
        // 删除cell中的子对象,解决覆盖问题。
        while ([cell.contentView.subviews lastObject] != nil) {
            [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];
        }
    }
原创粉丝点击