我创建了具有4个不同imageView的单元格,其中我创建了动态约束,因此它们将更改宽度和高度等于设备大小,但由于单元格不是动态的,因此它赢得了#39; t更改imageView的高度。因此,我想将单元格高度设置为等于图像宽度,因此图像的宽度和高度始终相同。
在heightForRowAtIndexPath中,我试图返回以下内容,但这给了我一个错误:(lldb)
let cell = tableView.dequeueReusableCellWithIdentifier("ImageViewCell", forIndexPath: indexPath) as ImageViewCell
return cell.image1.frame.width
我该怎么做?
答案 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
否则
为constraints
,top
,bottom
,left
,right
和width
约束添加height
,并使用> =条件用于高度约束。