【发布时间】:2016-04-22 10:44:22
【问题描述】:
我的应用有一个UITableViewController。我想在第一部分显示图像并在另一部分添加一些其他单元格。像这样:
-------
| | <--- image
| |
-------
blah blah blah <--- some other cells
--------------
blah blah blah
--------------
blah blah blah
我认为这很简单:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.section == irrelevant {
let cell = UITableViewCell()
let image = UIImageView(image: someImage, highlightedImage: nil)
cell.contentView.addSubview(image)
return cell
}
// Code for the other cells, irrelevant
}
但结果是这样的:
如您所见,图像已超出单元格!我想要做的是拉伸单元格,使图像实际上在单元格“内部”。我试过这个:
cell.sizeToFit()
还有这个:
cell.frame = CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.width, image.frame.height + 15)
但两者都不起作用(结果保持不变)!
有没有办法拉伸它?如果没有,如何在UITableViewController 中显示图像?
注意:不要告诉我设置单元格的imageView 属性。太小了!
【问题讨论】:
-
使用tableView委托方法heightForRowAtIndexPath
-
更改 heightForRowAtIndexPath 中的单元格高度
-
通过这个例子,你总是可以把所有的元素和单元格都得到很好的高度和表格视图:stackoverflow.com/a/35022751/4693765。更改字段值时重新加载。
标签: ios swift image uitableview