TechChain
TechChain

Reputation: 8952

table cell height issue iOS?

I have custom table view.I have added 4 views in that

1.ImageView 2.Three labels

enter image description here

Now I want that image should increase in size for multiple devices.For that i have given it below constraints.Proptional height is 176:339

enter image description here

Now in order to increase the height of the image i have increase height of table i used below code.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    if(!self.customCell)
    {
        self.customCell = [self.tableview dequeueReusableCellWithIdentifier:@"tableCell"];
    }
    CGFloat height;
    height=self.customCell.img_main.frame.size.height;
    height=height+self.customCell.label_one.frame.size.height;
    height=height+self.customCell.label_two.frame.size.height;
    height=height+self.customCell.label_three.frame.size.height;
    height=height+self.customCell.dev_name.frame.size.height;
    NSLog(@"height is %f",self.customCell.img_main.frame.size.height);

    return height;
}

The heigh of image is always 176 which i set in interface builder for 4 inch screen.

Why the height of both images & table view cell is not increasing ?

Upvotes: 1

Views: 363

Answers (1)

MudOnTire
MudOnTire

Reputation: 506

This is for your second question, if you want to change the image view's height base on the image it display, i suggest to use Height Constraint instead of Proportional Height Constraint, the calculation will be easier. When you get the image, and know the image size, so you calculate and get the size of the image view, and then update the Height Constrains. Done.

drag the constraint to your cell

enter image description here

update the constraint when image setted

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CustomeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellId" forIndexPath:indexPath];
    cell.image = image;
    cell.imageViewHeightConstraint.constant = [self calcuteSizeWithImage:image]; //you need to do the calculation yourself
    return cell;
}

Upvotes: 1

Related Questions