0

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.

mad_dog
  • 148
  • 6
  • From Xcode 6 you need to give autolayout constraints to custom UITableViewCell UI elements.I had a similar issue - fix is to set correct constraints. – bhavya kothari Oct 16 '14 at 04:28
  • bhavya... my UIElements do have constraints, so I am not sure exactly what you mean. the constraints appear to be correct anyway... I base this on what the "Debug View Heirarchy" output looks like in XCode. The images appear there in the correct position, but are not showing on the device. – mad_dog Oct 16 '14 at 05:18
  • Try to make person image view's background clear color (if that doesn't help, make cell background clear color). I had a bug with table view section view. I created view containing different subviews. Subviews were not visible as long as view's background was white color. When I changed it to clear color, subviews became visible. – Yevgeniy Leychenko Oct 24 '14 at 09:11
  • Thanks. But that didn't help. I tried cell, content view and imageview backgrounds as clear. :( – mad_dog Oct 24 '14 at 09:18
  • No unfortunately. I disabled and re-enabled auto layout and size classes and re-created those from scratch. Not a big job at the start of a project but would be a hassle if it affects a lot of screens. – mad_dog Feb 11 '15 at 03:18

3 Answers3

1

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();
Abhiram
  • 247
  • 2
  • 14
0

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.
   }
Danial Hussain
  • 2,488
  • 18
  • 38
  • cell.tag needs to be an NSInteger and cannot be an NSIndexPath. Likewise, the NSIndexPath indexPath can't be an NSInteger... and should be "NSIndexPath *indexPath" anyway. So... have you tested your answer? – mad_dog Oct 26 '14 at 06:23
  • thanks. but it makes no difference. i'm not sure what this is meant to achieve as it is just re-writing the same number in the indexpath.row. The data is there... it just isn't being drawn on the screen. – mad_dog Oct 26 '14 at 07:15
  • i think you should log the indexPath.row in both function. – Danial Hussain Oct 26 '14 at 09:20
  • The index path information is correct. As I have mentioned, the image information is actually present (as is the other information from the same core data entity which shows correctly). The image just isn't displayed on the screen until the cell is scrolled off screen and back on screen again. – mad_dog Oct 26 '14 at 09:33
0

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.

mad_dog
  • 148
  • 6