【问题标题】:How to make a UITableViewCell longer in height? [duplicate]如何使 UITableViewCell 的高度更长? [复制]
【发布时间】: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


【解决方案1】:

使用这个

override func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {

 if indexPath.section == 0 {
   return heightForYourImage
  }
  else if indexPath.section == 1 {
    return heightForYour labels
  }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多