2015-10-14 117 views
1

我知道这个问题已经被问过很多次了,但是我的问题有些不同。自定义单元格内容在UITableView中重叠

我在单元格的内容视图中以编程方式创建UIView和UIImageView。当TableView第一次出现时它看起来很完美,但是当我向下滚动时,这似乎是重叠的。

的截图不滚动:

滚动后

enter image description here

截图:

viewForHead = [[UIView alloc]initWithFrame:CGRectMake(cell.viewForContents.frame.origin.x, cell.viewForContents.frame.origin.y-10, cell.viewForContents.frame.size.width, 45)]; 
viewForHead.backgroundColor = [UIColor colorWithRed:232.0/255.0 green:255.0/255.0 blue:16.0/255.0 alpha:1]; 
[cell.contentView addSubview:viewForHead]; 

UIImageView *imageViewForDP = [[UIImageView alloc]initWithFrame:CGRectMake(viewForHead.frame.origin.x-50, viewForHead.frame.origin.y-8, 60,60)]; 
imageViewForDP.image = [UIImage imageNamed:@"dog_1.png"]; 
//[cell.viewForContents addSubview:imageViewForDP]; 
imageViewForDP.layer.cornerRadius = 30; 
imageViewForDP.clipsToBounds = YES; 
[viewForHead addSubview:imageViewForDP]; 

请让我从这个问题:我跟随

enter image description here

代码。谢谢

+0

检查,如果您使用的是可重复使用的电池,然后再次尝试添加它之前取消对单元格的内容视图中的所有子视图!你可以使用“for .. in”循环来实现 – Nayan

回答

2

使用此为您- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

if ([cell.contentView subviews]){ 
    for (UIView *subview in [cell.contentView subviews]) { 
     [subview removeFromSuperview]; 
    } 
} 
+1

不是最好的解决方案,因为每次cellForRow被调用时都需要重新创建整个单元。比起使用这种方法,将细胞放在第一位更好。更好的是重用单元格(如苹果预期的),并更新已更改的项目。 –

1

您每次单元格出列时都将viewForHead添加为子视图。所以你把它们加在一起。

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"]; 

    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"CELL"] autorelease]; 

//  This is where you CREATE your cell contents. 
     viewForHead = [[UIView alloc]initWithFrame:CGRectMake(cell.viewForContents.frame.origin.x, cell.viewForContents.frame.origin.y-10, cell.viewForContents.frame.size.width, 45)]; 
     viewForHead.backgroundColor = [UIColor colorWithRed:232.0/255.0 green:255.0/255.0 blue:16.0/255.0 alpha:1]; 
     [cell.contentView addSubview:viewForHead]; 

     UIImageView *imageViewForDP = [[UIImageView alloc]initWithFrame:CGRectMake(viewForHead.frame.origin.x-50, viewForHead.frame.origin.y-8, 60,60)]; 
     imageViewForDP.image = [UIImage imageNamed:@"dog_1.png"]; 
//  [cell.viewForContents addSubview:imageViewForDP]; 
     imageViewForDP.layer.cornerRadius = 30; 
     imageViewForDP.clipsToBounds = YES; 
     imageView.tag = 1 
     [viewForHead addSubview:imageViewForDP]; 

    } 

//  this is where you UPDATE your viewForHead image and any other elements inside your cell 
     UIImageView *imageView = [cell.contentView viewWithTag:1]; 
     imageView.image = // your new image 

    return cell; 

} 

子类你的UITableViewCell并与厦门国际银行建立您的布局甚至会更好,那么你可以只直接访问细胞的特性。更清洁的解决方案。

MyCustomCell *cell = [tableView [email protected]"CELL"]; 
if (cell == nil) { 
    cell = [[MyCustomCell alloc] init]; // you ID is set in interface builder 
} 

cell.imageView.image = // your new image here. 
cell.someLabel.text = @"some new text here" 
0

这个问题是因为表视图单元格将得到重用,你是对细胞再次添加视图。

试试下面的代码:

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"]; 

if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"] ; 

    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
} 

UIView *viewForHead = (UIView *)[cell.contentView viewWithTag:1]; 
if (viewForHead==nil) { 

    viewForHead = [[UIView alloc]initWithFrame:CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width, 20)]; 
    viewForHead.backgroundColor = [UIColor colorWithRed:232.0/255.0 green:255.0/255.0 blue:16.0/255.0 alpha:0.5]; 
    viewForHead.tag = 1; 
    [cell.contentView addSubview:viewForHead]; 
} 
return cell;}