首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >滚动时复制UITableViewCell contentView的子视图(UIButton和UIImageView)

滚动时复制UITableViewCell contentView的子视图(UIButton和UIImageView)
EN

Stack Overflow用户
提问于 2012-10-17 20:29:11
回答 3查看 1.2K关注 0票数 3

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

代码语言:javascript
运行
复制
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];
        }
    }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-10-19 17:00:40

现在问题已经解决了。我在创建新tableViewCell的else用例中使用了下面这行代码。

代码语言:javascript
运行
复制
[cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

我编辑了我的代码,如下所示:

代码语言:javascript
运行
复制
if (cell == nil) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    else
    {
        [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    }
票数 10
EN

Stack Overflow用户

发布于 2012-10-17 20:35:50

尝试下面的代码..

代码语言:javascript
运行
复制
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
EN

Stack Overflow用户

发布于 2012-10-17 20:42:43

正如Romit Mewada建议的那样,你可以使用它,但是每次你有了alloc并添加新的UIImageView

相反,您可以将nil发送到该UIImageView的映像。

以这种方式实现。

代码语言:javascript
运行
复制
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.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12934364

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档