0

I have a UITableView app that consists of text and images. The cells sometimes overwrite themselves and if I scroll the problem gets worse. See http://web-cars.com/images/temp_img/Simulator-Screen-Shot.png

I had the same problem in a previous app and a friend showed me how to resolve it by subclassing UITableViewCell. That was a text only app and I am unable to get the same solution with the images.

Here is my subclassed UITableViewCell:

-(void)prepareForReuse  {
    self.permLabel101.text = @"";
    self.permUIImageView201.image = nil;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier    {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    UILabel *label101 = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 310, 100)];
    label101.tag = 101;
    [label101 setLineBreakMode:NSLineBreakByWordWrapping];
    [label101 setFont:[UIFont systemFontOfSize:FONT_SIZE]];
    label101.textColor = [UIColor blackColor];
    label101.backgroundColor = [UIColor clearColor];
    label101.numberOfLines = 0;

    self.permLabel101 = label101;

    [self.contentView addSubview:self.permLabel101];

    UIImageView *image201 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 100)];
    image201.tag = 201;

    return self;
}

Here is my cellForRowAtIndexPath. I divide the text and images with photo_text.

- (MyTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSManagedObject *object = (NSManagedObject *)[entityArray objectAtIndex:indexPath.row];

    NSString *CellIdentifier = @"Cell";

    int photo_text = [[object valueForKey:@"photo_text"]intValue];

    MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)   {
        cell = [[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView setSeparatorColor:[UIColor clearColor]];    // Eliminates border between cells

    if (photo_text == 1)    {   //  Image will be here

        NSNumber *imgHeight = [[entityArray objectAtIndex:indexPath.row]valueForKey:@"imgHeight"];
        float imgHeightFloat = [imgHeight floatValue];
        NSLog(@"imgHeightFloat: %f", imgHeightFloat);

        UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(0, 2, 320, imgHeightFloat)];    //  imv is OK
        UIImageView *image201View = [[UIImageView alloc]initWithFrame:CGRectMake(0, 2, 320, imgHeightFloat)];

        imv.image = [UIImage imageNamed:[object valueForKey:@"photo"]];
        image201View.image = [UIImage imageNamed:[object valueForKey:@"photo"]];

        NSLog(@"Image cell: %@", cell);

        NSString *title = [object valueForKey:@"title"];
        self.title = title;
        [cell.contentView addSubview:image201View];

        return cell;

    }   else if (photo_text == 2)       {   //  Text will be here

        UILabel *label101;

        cell.selectionStyle = UITableViewCellSeparatorStyleNone;
        [self.tableView setSeparatorColor:[UIColor clearColor]];    //  Eleminates border between cells

        NSString *text = [object valueForKey:@"text"];

        CGSize constraint = CGSizeMake(296.0f, 20000.0f);
        CGSize size = [text sizeWithFont: [UIFont systemFontOfSize:12.0f] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];

        NSLog(@"Text cell: %@", cell);

        label101 = (UILabel *)[cell viewWithTag:101];

        label101.frame = CGRectMake(5, 5, 320, size.height);
        label101.text = [NSString stringWithFormat:@"%@", text];

        NSString *title = [object valueForKey:@"title"];
        self.title = title;

        return cell;
    }
        // Configure the cell...
//    return cell;
}

Suggestions are appreciated!!

  • cell reuse issue. These will help. https://stackoverflow.com/questions/52258956/view-is-getting-replaced-while-scrolling-in-uitableview/52259714#52259714, https://stackoverflow.com/questions/50916739/reusing-cells-in-uitableview/51152042#51152042 – Rakesha Shastri Dec 30 '18 at 08:05
  • While preparing for cell reuse ```self.permUIImageView201.image = nil;``` will have no effect as there is no reference! and each time when cell is being reused ``` cellForRowAtIndexPath``` is adding image view to cell without removing the previous one is causing the problem. – vignesh Dec 31 '18 at 05:56
  • @vignesh: I thought that -(void)prepareForReuse in MyTableViewCell would remove the image thus preventing its reuse but I see now that it does not. I've tried image201View.image = nil; in cellForRowAtIndexPath and in MyTableViewCell but no luck there. I agree that I am adding an image without removing the previous one but am at a loss as to how to remove that image. Any suggestions? – safari yellow Dec 31 '18 at 19:52
  • @safariyellow assigning a reference with nil would not remove view added as subview, what you need to do is removeSubview with tag upon cell reuse. – vignesh Jan 04 '19 at 08:47

1 Answers1

0

A friend was able to point me in the right direction.

My main problem was that I had a CellIdentifier conflict.

I had the following before my if statement separating the image and text cells:

NSString *CellIdentifier = @"cell";

And I needed to use different CellIdentifiers for the image and text cells:

NSString *CellIdentifier = @"cell";

and NSString *CellIdentifier = @"cell2";

Also it turns out I did not need to place my UILabel and UIImageView in the subclassed UITableViewCell.

Finally, to get rid of a warning on cellForRowAtIndexPath: I needed

`        return cell;
}   else    {
    return nil;
}`