BergP
BergP

Reputation: 3545

Size Class Customization in UITableViewCell

I have a height constraint in UIImageView which is contained in UITableViewCell, and I want it to be 180 for iPhone and 300 for iPad.

enter image description here enter image description here

But it doesn't make any effect for iPad.

It is a table view with automatic dimension.

- (void)configureTableView {
    self.tableView.allowsSelection = NO;
    self.tableView.estimatedRowHeight = 30.f;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
}

How can I customize cell's height for iPad?

update: I fixed it by implementing delegate method:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat height = -1;
    if (indexPath.section == kQuizControllerSection_Description && indexPath.row == kDescriptionQuizCell_PreviewImage) {
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            height = 400;
        }
        else {
            height = 180;
        }
    }
    return height;
}

because other cells are resized dynamically (cells with multiline labels) method returns negative number for every other cell. Is it correct approach?

Upvotes: 2

Views: 249

Answers (2)

Bhavin Bhadani
Bhavin Bhadani

Reputation: 22374

I there is only UIImageView with the same height as cell ....then just pinned all edges of UIImageView and then there is not any need of height constraint of UIImageView...

You just need to set height of cell according to device in heightForRowAtIndexPath method

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    if UIDevice.currentDevice().userInterfaceIdiom == .Pad{
        return 300
    }
    else{
        return 180
    }
}  

The other scenario where UIImageView is not the same height as cell....then make a one IBOutlet of height constraint of UIImageView and then change the constant as per your requirement...

    if UIDevice.currentDevice().userInterfaceIdiom == .Pad{
        heightconstraint.constant = 300
    }
    else{
        heightconstraint.constant = 180
    }

Upvotes: 2

Simon Degn
Simon Degn

Reputation: 968

This will do it:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // default height is 180
    int height = 180;

    // if your device is an iPad then it's 300
    if (UIDevice.currentDevice().model.rangeOfString("iPad") != nil)
        height = 300;

    // if you want the UIImageView to follow the height, just go with this:
    yourImageView.frame = CGRectMake(0, 0, tableView.frame.size.width, height);

    return height;
}

Upvotes: 1

Related Questions