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

后端 未结 3 1164
温柔的废话
温柔的废话 2020-12-07 03:57

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

相关标签:
3条回答
  • 2020-12-07 04:20

    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)];
        }
    
    0 讨论(0)
  • 2020-12-07 04:22

    Try this below code..

    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)
  • 2020-12-07 04:26

    As Romit Mewada suggested you can use that but each time you have alloc & add new UIImageView.

    Instead of that you can send nil to the image of that UIImageView.

    Implement in this way.

    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 讨论(0)
提交回复
热议问题