单元格高度等于图像宽度

时间:2014-10-18 20:37:57

标签: ios objective-c iphone swift

我创建了具有4个不同imageView的单元格,其中我创建了动态约束,因此它们将更改宽度和高度等于设备大小,但由于单元格不是动态的,因此它赢得了#39; t更改imageView的高度。因此,我想将单元格高度设置为等于图像宽度,因此图像的宽度和高度始终相同。

在heightForRowAtIndexPath中,我试图返回以下内容,但这给了我一个错误:(lldb)

let cell = tableView.dequeueReusableCellWithIdentifier("ImageViewCell", forIndexPath: indexPath) as ImageViewCell

return cell.image1.frame.width

我该怎么做?

2 个答案:

答案 0 :(得分:0)

我认为您需要使用Tableview的委托来设置其单元格高度。也许你可以试试这样的事情:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
     switch (indexPath.row) {
    case 0:
        return image.frame.size.width;
        break;
    case 1:
        return image.frame.size.width;
        break; 
    default:
        return 44;
        break;
     }
}

我认为您需要从数据源中获取UIimage的大小。不是来自你的牢房。

答案 1 :(得分:0)

您无法在heightForRowAtIndexPath内调用单元格对象,因为它会在cellForRowAtIndexPath调用heightForRowAtIndexPath时创建无限循环。其次,使用约束调整width 因为功能名称清楚地表明它是高度。

如果您确定控件的高度,请使用此

return 90.0 //its optional if height is fixed 

否则
constraintstopbottomleftrightwidth约束添加height,并使用> =条件用于高度约束。

相关问题