【问题标题】:Swift: how to set UITableViewCell in cellForRowAtIndexPath methodSwift:如何在 cellForRowAtIndexPath 方法中设置 UITableViewCell
【发布时间】:2016-10-29 16:34:01
【问题描述】:

在我的UITableViewController 中,我有许多自定义单元格,其高度取决于其中的 UITextView 的长度。这意味着我无法从 heightForRowAtIndexPath() 方法返回精确的大小,因为那时我仍然不知道。此外,由于我无法为支配UITextView 的约束创建@IBOutlet weak var,因此这是我尝试过的代码:

let cell =  tableView.dequeueReusableCellWithIdentifier("BigTextCell", forIndexPath: indexPath) as! BigTextCell

cell.backgroundColor = UIColor(patternImage: UIImage(named: "icons/background.png")!)

cell.paragraph.layer.borderColor = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 1).CGColor
cell.paragraph.layer.borderWidth = 0.5
cell.paragraph.editable = false
cell.title.enabled = false


switch (indexPath.row) {
  case 1:
        cell.title.text = "Chi è:"
        cell.icon.image = UIImage(named: "icons/field_fish.png")
        cell.paragraph.text = self.fieldsCollection["life_method"] as! String
        cell.paragraph.backgroundColor = UIColor(patternImage: UIImage(named: "icons/quadretti.png")!)

        for constraint in cell.icon.constraints {
          if (constraint.identifier == "icon_width") {
            constraint.constant = 64.0
            break
          }
        }

        for constraint in cell.paragraph.constraints {
          if (constraint.identifier == "longTextConstraint") {
            constraint.constant = cell.paragraph.sizeThatFits(CGSize(width: cell.paragraph.frame.width, height: CGFloat.max)).height
            break
          }
        }
...

因此,UITextView 根据文本的长度正确调整大小,但通常单元格的高度太大。我怎样才能解决这个问题? 谢谢

更新 正如你们中的一些人所建议的,如果我在heightForRowAtIndexPath 中尝试这个

if (indexPath.row == 1) {
        //return 235.0
        let cell =  tableView.dequeueReusableCellWithIdentifier("BigTextCell", forIndexPath: indexPath) as! BigTextCell
        cell.paragraph.text = self.fieldsCollection["life_method"] as! String

        for constraint in cell.paragraph.constraints {
            if (constraint.identifier == "longTextConstraint") {
                constraint.constant = cell.paragraph.sizeThatFits(CGSize(width: cell.paragraph.frame.width, height: CGFloat.max)).height
                return constraint.constant+30.0
                break
            }
      }
}

模拟器方块

【问题讨论】:

    标签: ios xcode swift uitableview


    【解决方案1】:

    在你的 viewDidLoad 方法中试试这个:

        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 100.0 //Change this to any value
    

    【讨论】:

    • 恐怕它不起作用,因为我得到的单元格高度相同,而且非常低......也许我的约束不正确,但我不知道在哪里更正它们......
    【解决方案2】:

    您可以(并且应该)返回 - tableView:heightForRowAtIndexPath:- tableView:estimatedHeightForRowAtIndexPath: 的确切高度(此处不一定是确切高度,但越接近越好,如果值不够精确,您可能会遇到视觉故障和问题自动布局,如果你正在使用它)。计算中唯一缺少的是单元格实例,您可以通过调用相应的方法 cellForRowAtIndexPath 或使用从不显示但用于计算的静态单元格实例从表视图中获取它。前者快速且易于使用,后者有自己的特定用例,例如如果您出于某种原因想要在没有实际表格视图的情况下计算单元格的高度。

    注意虽然UITableViewUITableViewDataSource有类似签名的方法,分别是- cellForRowAtIndexPath - tableView:cellForRowAtIndexPath:,但是它们的作用不同,table view的方法可以返回nil,参考文档详情。

    【讨论】:

    • 我已经试过你写的它不起作用。我的意思是我试图在heightForRowAtIndexPath() 中实例化一个单元格,然后根据长度调整UITextView 的约束,最后返回类似return constraint.constant+30.0 的东西,但模拟器阻塞了......
    • 不要从tableView:heightForRowAtIndexPath 内部将单元格出列,这是无限递归的潜在原因:iOS 可能希望在您出列单元格时调用tableView:heightForRowAtIndexPath:。通常,如果文档没有直接说明您应该这样做,请避免某些委托方法对同一委托的其他方法的不必要依赖。尝试调用表格视图的cellForRowAtIndexPath:。或者手动初始化单元类的单个静态实例,仅用于计算。
    • 恐怕我不能调用cellForRowAtIndexPath,因为它总是为每个indexPath.row 出列一个单元格...我正在尝试初始化一个自定义单元格进行计算,但我没有得到正确的高度。 ..也许是因为它缺乏dequeueReusableCellWithIdentifier 调用中的约束...
    【解决方案3】:

    您可以在每个heightForRowAtIndexPath() 方法调用中设置tableView.rowHeight = UITableViewAutomaticDimension 或计算适当文本的单元格高度。

    【讨论】:

    • 那么,我是否必须在heightForRowAtIndexPath 中重复我在cellForRowAtIndexPath 执行的相同指令?此外,如果我使用UITableViewAutomaticDimension,我会得到所有具有相同高度的单元格......
    • 你真的需要 UITextView 吗?你可以使用可以根据文本调整大小的 UILabel 吗?在您的 heightForRowAtIndexPath 中,您应该获取当前索引路径的文本并为其计算文本高度(请参阅link)。
    • 使用UITableViewAutomaticDimension时需要在tableView:estimatedHeightForRowAtIndexPath:处返回一个有意义的值,如果你这样做了,它仍然不起作用,你应该检查约束。请记住,在表格视图中使用自动维度虽然非常方便,但并非没有错误,请做好准备并准备好使用旧的替代方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    • 1970-01-01
    相关资源
    最近更新 更多