我的应用有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
属性。太小了!
答案 0 :(得分:1)
使用此
override func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
if indexPath.section == 0 {
return heightForYourImage
}
else if indexPath.section == 1 {
return heightForYour labels
}
}