<分区>
标签 ios swift image uitableview
<分区>
我的应用程序有一个 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
}
但是结果是这样的:
如您所见,图像在单元格外!我想做的是拉伸(stretch)单元格,使图像实际上位于单元格“内部”。我试过这个:
cell.sizeToFit()
还有这个:
cell.frame = CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.width, image.frame.height + 15)
但是两者都不起作用(结果保持不变)!
有没有办法拉伸(stretch)它?如果没有,如何在 UITableViewController
中显示图像?
注意:不要告诉我设置单元格的 imageView
属性。太小了!
最佳答案
使用这个
override func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
if indexPath.section == 0 {
return heightForYourImage
}
else if indexPath.section == 1 {
return heightForYour labels
}
}
关于ios - 如何使 UITableViewCell 的高度更长?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34834795/