3

在我的 tableView 中,在某些单元格中,我添加了一个 imageView 作为单元格 contentView 的子视图。在上下滚动 tableView 时,这些图像也会复制到其他单元格上。但这个问题并不总是发生。请提出解决方案..我正在使用以下代码。

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];
        }
    }
}
4

3 回答 3

10

现在问题已经解决了。我在创建新 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)];
    }
于 2012-10-19T09:00:39.767 回答
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];
          }
     }
  }
}
于 2012-10-17T12:35:49.593 回答
0

正如 Romit Mewada 建议的那样,您可以使用它,但每次您都有 alloc & add new UIImageView

取而代之的是,您可以将 nil 发送到 that 的图像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.
于 2012-10-17T12:42:42.903 回答