滚动时复制UITableViewCell contentView的子视图(UIButton和UIImageView) [英] Duplicating subviews(UIButton and UIImageView) of UITableViewCell contentView on scrolling

查看:126
本文介绍了滚动时复制UITableViewCell contentView的子视图(UIButton和UIImageView)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my tableView, in some cells i have added an imageView as subview of cell contentView. On scrolling tableView up and down these images duplicating on other cells also. But this problem doesn't occur always. Please suggest a solution.. I am using the following code.

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

推荐答案

问题已解决.在创建新的tableViewCell的else情况下,我使用了以下行.

The problem is solved now. I have used the following line inside the else case of creating new tableViewCell.

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

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

I have edited my code as shown below :

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

这篇关于滚动时复制UITableViewCell contentView的子视图(UIButton和UIImageView)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆