Reusable UITableviewcell not displaying initial images correctly

757 Views Asked by At

So I have a UITableview with Dynamic prototypes. The cells are custom cells with 4 properties:

  • imageVBkg (background image)
  • mainText (label)
  • subtitle (label)
  • personImageView (uiimageview with the person's photo).

I had this working fine in my original storyboard, but have recently moved to a new storyboard. (FYI I cannot re-test with the old storyboard as there have been some changes to code that preclude this).

** THIS SEEMS TO BE AN AUTOLAYOUT ISSUE. DISABLING AUTOLAYOUT FIXES THE PROBLEM (BUT AUTOLAYOUT IS THERE FOR A REASON). **

Basically, the issue is this:

  • the label content shows fine (data is taken from a fetchedresultscontroller/coredata)
  • imageVBkg works fine
  • logging reports that the images do actually exist
  • List item
  • when initially displayed, the personImageView contents are not visible
  • once i scroll and "reuse" the cells, the personImageView contents appear correctly.

** EDIT **

  • if I select a cell and go to the next screen, then hit the save button, the app returns to the original tableview screen, and the images appear. it doesn't really make sense that updating the coredata info would make the images appear though.

Some example code below...

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PersonCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"personCell" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[PersonCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"personCell"];
    }
    cell.delegate = self;
    cell.dataSource = self;
    cell.backgroundColor = _blueColor;

    [self configureCell:cell atIndexPath:indexPath];
    return cell;
}

-(void)configureCell:(PersonCell *)cell atIndexPath:(NSIndexPath *)indexPath
{

    Person *person = [_fetchedResultsController objectAtIndexPath:indexPath];

    cell.imageVBkg.image = [[UIImage imageNamed:@"list-item-background"] resizableImageWithCapInsets:UIEdgeInsetsMake(50, 50, 30, 30)];
    cell.mainText.text = [NSString stringWithFormat:@"%@ %@", person.firstname, person.lastname];
    Company *company = (Company *)person.worksFor;
    NSString *fullString = person.position;
    if ([company.name length] > 0 && [person.position length] > 0) {
        fullString = [fullString stringByAppendingString:@" at "];
    }
    if (company.name != nil) {
        fullString = [fullString stringByAppendingString:company.name];
    }

    cell.subtitle.text = fullString;

    if ([person hasPhoto]) {
        _image = [person photoImage];
        if (_image != nil) {
            cell.personImageView.clipsToBounds = YES;
            cell.personImageView.image = _image;
            cell.personImageView.layer.cornerRadius = cell.personImageView.bounds.size.width / 2.0f;
        }
    }
}

PersonCell.h has the following properties:

@property (nonatomic, weak) IBOutlet UILabel *mainText;
@property (nonatomic, weak) IBOutlet UILabel *subtitle;
@property (nonatomic, weak) IBOutlet UIImageView *personImageView;
@property (strong, nonatomic) IBOutlet UIImageView *imageVBkg;

Also worth mentioning... If I don't use "cell.personImageView.clipsToBounds = YES;" then the personImageView images DOES appear, but as a square, not as a circle, which is how I need it.

Hopefully this is a simple fix...

Thanks in advance for any suggestions.

3

There are 3 best solutions below

4
On

First you need to tag your cell.

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PersonCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"personCell" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[PersonCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"personCell"];
    }
    cell.delegate = self;
    cell.dataSource = self;
    cell.backgroundColor = _blueColor;

   //See these two lines 
   //Edit
    cell.tag = indexPath.row;

   [self configureCell:cell];

   return cell;
}

2nd you had to change your configure cell method like this.

  -(void)configureCell:(PersonCell *)cell
  {
   //Edit
    NSInteger index = (NSInteger) (cell.tag)
    NSIndexPath *path = [NSIndexPath indexPathWithIndex:index];

    // after this place your code.
   }
0
On

Completely disabling autolayout and size classes and then re-enabling and configuring them all again has fixed it for some reason. Just removing constraints did not work. No idea what the real issue was. Suggestions welcome.

0
On

use this code at cell for row here 122,111 are THE frames for image view in table view cell

 CGSize itemSize = CGSizeMake(122,111);    
 UIGraphicsBeginImageContextWithOptions(itemSize, NO, UIScreen.mainScreen.scale);           
    CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);     
    [cell.imageView.image drawInRect:imageRect];             
  cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();      
    UIGraphicsEndImageContext();