Reputation: 97
if (indexPath.row == 2)
{
RestaurantTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RestaurantCell"];
if (!cell)
{
[tableView registerNib:[UINib nibWithNibName:@"RestaurantCell" bundle:nil] forCellReuseIdentifier:@"RestaurantCell"];
cell = [tableView dequeueReusableCellWithIdentifier:@"RestaurantCell"];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = cell.gradientView.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor clearColor] CGColor], (id)[[UIColor blackColor]CGColor], nil];
[cell.gradientView.layer insertSublayer:gradient atIndex:0];
cell.gradientView.alpha = 0.9;
}
NSData *imgData2 = [NSData dataWithData:restaurantPhotoArray[1]];
cell.restaurant2Photo.image = [UIImage imageWithData:imgData2];
cell.restaurant2Photo.contentMode = UIViewContentModeScaleAspectFill;
[cell.restaurant2Photo setClipsToBounds:YES];
NSDictionary *morning2 = [[NSDictionary alloc]initWithDictionary:NorthRestaurantsArray[1]];
cell.restaurant2Name.text = morning2[@"name"];
cell.restaurant2Name.numberOfLines = 0;
[cell.restaurant2Name sizeToFit];
}
I have a custom cell that is implemented in cellForRowAtIndexPath with the code above. When the table is displayed, restaurant2Photo displays the specified image and the correct text is set to restaurant2Name, however sizeToFit has no effect on the label so its size stays constant, as defined in .xib file. I have another custom cell which is displayed at indexPath 0 and 1 where sizeToFit works fine, however when it gets to indexPath 2 and RestaurantTableViewCell sizeToFit does not work. Is anyone able to suggest why this could be happening? Thanks for any help in advance :)
Upvotes: 0
Views: 69
Reputation: 5343
Probably your nib file has AutoLayout enabled.
In your custom cell awakeFromNib:
method write the following
[self.restaurant2Name setTranslatesAutoresizingMaskIntoConstraints:YES];
and this would do the job for you.
Upvotes: 2