如何让UITableViewCell的高度更长?

时间:2016-01-17 03:27:45

标签: 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
}

但它出现了这样:

enter image description here

如您所见,图像不在单元格中!我想要做的是拉伸细胞,使图像实际上在细胞内“。我试过这个:

cell.sizeToFit()

和此:

cell.frame = CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.width, image.frame.height + 15)

但两者都不起作用(结果保持不变)!

有没有办法伸展它?如果没有,我如何在UITableViewController

中显示图像

注意:不要告诉我设置单元格的imageView属性。太小了!

1 个答案:

答案 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
  }

}
相关问题