Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
611 views
in Technique[技术] by (71.8m points)

objective c - Duplicating subviews(UIButton and UIImageView) of UITableViewCell contentView on scrolling

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];
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...