0
votes

I have a UIImageView inside a custom cell. If there is no image to display in cell then cell height should automatically decrease.(ImageView Height should be zero in that case). How to specify constraint for this in xib ?

2
Does images vary in height, or are they always the same? Have you added constraints from top of image to top of cell, and the same for the bottom? - MartinHN
hi please see this question : stackoverflow.com/questions/30896856/… - user1865227

2 Answers

1
votes

Your UITableView data source should represent the existence of this image. You should use the following delegate method:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.dataSource[indexPath.rox].imageExists) {
        return 50.0; // Change to your value with image
    }
    return 10.0; // Change to your value without image
}
0
votes

Your UITableViewDelegate should implement tableView:heightForRowAtIndexPath:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // add your condition here if image exit or not and set cell size dynamically
    return [indexPath row] * 20;
}

You will probably want to use NSString's sizeWithFont:constrainedToSize:lineBreakMode: method to calculate your row height rather than just performing some silly math on the indexPath.

Here is good sample code for dynamic size cell.