2015-10-14 114 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;}