ios - 如何使 UITableViewCell 的高度更长?

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

如您所见,图像在单元格外!我想做的是拉伸(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/

上一篇:ios - UIButton 导致崩溃

下一篇:ios - 重新加载 View Controller 并以编程方式更改 UIImageView 约束

相关文章:

image - 如何改进关键点检测和匹配

ios - SCNNodes 中的颜色

swift - 在 Swift 中设置两周时间表

ios - 如何增加特定部分中特定单元格的高度

javascript - 在加载页面之前根据屏幕分辨率调整所有图像的大小

image - 如何使图像背景透明?

iPhone Cocos2D - facebook、twitter 和 flickr

iphone - 在 iPhone 中使用 powerplot 创建折线图

iOS - 以编程方式移动/裁剪 UIView

objective-c - 如何以编程方式设置 WKInterfaceButton 字体?