2012-10-17 50 views
3

在我的tableView中,在一些單元格中,我添加了一個imageView作爲單元格contentView的子視圖。在滾動tableView上下複製其他單元格上的這些圖像也。但是這個問題並不總是會發生。請提出解決方案..我使用下面的代碼。UITableViewCell contentView在滾動時複製子視圖(UIButton和UIImageView)

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 

    if (friendsArray.count != 0) 
    { 
     NSString *str = [friendsIdArray objectAtIndex:indexPath.row]; 
     if ([pendingRequests containsObject:str]) 
     { 
      // Add image for pending item 
      UIImageView *pendImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pendng.png"]]; 
      pendImage.frame = CGRectMake(220.0, 2.5, 70, 40); 
      pendImage.tag = indexPath.row; 
      [cell.contentView addSubview:pendImage]; 
     } 
    } 

} 

NSString *object; 

if (friendsArray.count == 0) 
{ 
    if ([cell.contentView viewWithTag:indexPath.row]) 
    { 
     for (UIView *subview in [cell.contentView subviews]) 
     { 
      [subview removeFromSuperview]; 
     } 
    } 

    object = @"No friends added to the list"; 
    cell.textLabel.textAlignment = UITextAlignmentCenter; 
} 
else 
{ 
    object = [friendsArray objectAtIndex:indexPath.row]; 
    cell.textLabel.textAlignment = UITextAlignmentLeft; 

    if (![cell.contentView viewWithTag:indexPath.row]) 
    { 
     NSString *str = [friendsIdArray objectAtIndex:indexPath.row]; 
     if ([pendingRequests containsObject:str]) 
     { 
      // Add image for pending item 
      UIImageView *pendImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pendng.png"]]; 
      pendImage.frame = CGRectMake(220.0, 2.5, 70, 40); 
      pendImage.tag = indexPath.row; 
      [cell.contentView addSubview:pendImage]; 
     } 
    } 
} 

回答

8

現在問題已解決。我在創建新的tableViewCell的else情況下使用了以下行。

[cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; 

我已經編輯我的代碼如下所示:

if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
    } 
    else 
    { 
     [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; 
    } 
+1

+1好試試。工作正常 –

+0

@AnushaK,我試過這種方式,但沒有爲我工作,任何其他解決方案?如果是這樣,請回復 –

+0

customTableViewCell * cell =(customTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 如果(cell == nil){UINib * nib = cell = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0]; } IM實際上做這種方式,我試圖以上烏爾解決方案,但是沒有幫助 –

0

試試這個下面的代碼..

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 

    if (friendsArray.count != 0) 
    { 
     NSString *str = [friendsIdArray objectAtIndex:indexPath.row]; 
     if ([pendingRequests containsObject:str]) 
     { 
      // Add image for pending item 
      UIImageView *pendImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pendng.png"]]; 
      pendImage.frame = CGRectMake(220.0, 2.5, 70, 40); 
      pendImage.tag = indexPath.row; 
      [cell.contentView addSubview:pendImage]; 
     } 
    } 


    NSString *object; 

    if (friendsArray.count == 0) 
    { 
     if ([cell.contentView viewWithTag:indexPath.row]) 
     { 
      for (UIView *subview in [cell.contentView subviews]) 
      { 
      [subview removeFromSuperview]; 
      } 
     } 

     object = @"No friends added to the list"; 
     cell.textLabel.textAlignment = UITextAlignmentCenter; 
    } 
    else 
    { 
     object = [friendsArray objectAtIndex:indexPath.row]; 
     cell.textLabel.textAlignment = UITextAlignmentLeft; 

     if (![cell.contentView viewWithTag:indexPath.row]) 
     { 
      NSString *str = [friendsIdArray objectAtIndex:indexPath.row]; 
      if ([pendingRequests containsObject:str]) 
      { 
      // Add image for pending item 
      UIImageView *pendImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pendng.png"]]; 
      pendImage.frame = CGRectMake(220.0, 2.5, 70, 40); 
      pendImage.tag = indexPath.row; 
      [cell.contentView addSubview:pendImage]; 
      } 
    } 
    } 
} 
+0

此代碼不是爲我工作。仍然圖像在滾動的不同行中錯位。你可以建議任何其他解決方案? –

0

由於Romit Mewada建議您可以使用但每次你有時間的alloc &添加新UIImageView

相反的,您可以發送零到UIImageView的形象。

以這種方式實施。

if (cell == nil) 
{ 
    //get your cell or alloc & initialize 
    // create and add imageView 
} 

UIImageView* imageView = [cell.contentView viewWithTag:indexPath.row]; 
imageView.image = nil; 
//do your other task, you can use the same imageView to add other image for other row. 
相關問題