UITableViewCell中的大小类自定义

时间:2015-07-16 20:52:58

标签: ios objective-c uitableview ipad autolayout

我在UIImageView中有一个高度约束,它包含在UITableViewCell中, 我希望iPhone为180,iPad为300。

enter image description here enter image description here

但它对iPad没有任何影响。

这是一个具有自动尺寸的表格视图。

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

如何为iPad自定义单元格的高度?

更新 我通过实现委托方法来修复它:

- (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;
}

因为其他单元格动态调整大小(具有多行标签的单元格)方法会为每个其他单元格返回负数。这是正确的方法吗?

2 个答案:

答案 0 :(得分:2)

我只有与单元格高度相同的UIImageView ....然后只是固定UIImageView的所有边缘,然后就不需要UIImageView的高度约束...

您只需要根据heightForRowAtIndexPath方法

中的设备设置单元格的高度
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

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

另一种场景,其中UIImageView与单元格的高度不同....然后制作一个UIImageView高度约束的IBOutlet,然后根据您的要求更改常量......

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

答案 1 :(得分:1)

这样做:

- (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;
}