【问题标题】:Get height of each rows of UITableView获取UITableView每一行的高度
【发布时间】:2016-01-20 12:08:57
【问题描述】:

有什么方法可以获取UITableView 中每一行的行高。我有一个 UITableView 包含动态数据,我正在使用

self.tableView.rowHeight = UITableViewAutomaticDimension

制作动态行高。

但我也想更改整体UITableView 的高度,因为我需要重新加载数据后所有行的高度。

我尝试过使用

self.tableView.layoutIfNeeded()
self.tableViewHeightContraint.constant = self.tableView.contentSize.height

但由于未知原因,在我在UITableView 中添加 3 或 4 条记录之后,它给我的高度低于它的实际高度,所以考虑换一种方式。计算每行的高度,然后将它们相加。

我正在使用 Xcode 7.2、iOS 9 和 Swift 2

【问题讨论】:

  • 你是否尝试过像CGRect rect = [tableView rectForRowAtIndexPath:indexPath];这样的单元格矩形
  • 你能帮我快速处理这段代码吗?
  • 我是 swift 新手,但我想这会奏效。 let rect = self.tableView.rectForRowAtIndexPath(indexPath)

标签: ios swift uitableview


【解决方案1】:
var dictionaryOfCellHeight = [Int : CGFloat]()
var dataArray = [String]()
func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {


    if dictionaryOfCellHeight[indexPath.row] == nil
    {
    var frame : CGRect = tableView.rectForRowAtIndexPath(indexPath)

        if indexPath.row == dataArray.count
        {

            dictionaryOfCellHeight[indexPath.row] = frame.size.height
            //calculate total height and reload table
        }
        else
        {
        dictionaryOfCellHeight[indexPath.row] = frame.size.height
        }

    }
}

【讨论】:

  • 在执行此操作时,我在 if dictionaryOfCellHeight[indexPath.row] == nil 中遇到错误。错误是 = 警告:无法加载任何 Objective-C 类信息。这将显着降低可用类型信息的质量。
  • 如果需要,可以调用 cellforRow 中的代码,'dictionaryOfCellHeight[indexPath.row] == nil' 的目的是检查重复键。关键是 indexpath.row 应该是唯一的。我没有测试过代码
  • 也试过了,在 cellForRow 中没有出现错误,但应用程序冻结并且应用程序的内存占用不断增加并超过 2 GB,并且表格永远不会加载
【解决方案2】:

在 Swift 3.0 中

让 myRowHeight = yourTableView.rowHeight

【讨论】:

    【解决方案3】:

    要更改Tableview 的高度,您不需要计算所有UITableViewCells 的高度并设置为TableView。 在heightForRowAtIndexPath方法中正确设置每个UITableViewCellheight。这会自动将可滚动高度设置为您的Tableview

    【讨论】:

    • 滚动工作正常。但我不想滚动发生。这就是为什么试图改变UITableView的高度
    • 不想滚动效果加tableview的目的是什么?
    • 对多条记录重复相同的设计并在点击它们时执行一些操作。
    • @Abhi 如果你只是想防止滚动,为什么不使用scrollEnabled
    • @0x7fffffff 还有,我需要定义我的UITableView 的高度,否则某些记录可能永远不可见
    【解决方案4】:

    你可以用这个

    CGRect frame = [tableView rectForRowAtIndexPath:indexPath];
    NSLog(@"row height : %f", frame.size.height);
    

    使用frame.size.height 可以获得特定行的高度

    【讨论】:

      【解决方案5】:

      这是因为self.tableView.contentSize.height 返回的行数 * estimatedRowHeight,并不等于表格视图的实际高度。您需要通过visibleCells 获取各个单元格的高度,然后将它们相加得到表格视图的高度。

      参考答案: https://stackoverflow.com/a/40081129/4076956

      【讨论】:

        【解决方案6】:

        也许表格没有调整其内容的大小,所以如果您尝试使用[self. tableView layoutSubviews]; 强制调整大小,它可能会起作用。 [self. tableView layoutIfNeeded]; 不一定强制更新。

        【讨论】:

          【解决方案7】:

          功能上(单行)?

          注意:我的部分数组将行存储在 .data var 中

          let heightOfAllRows:CGFloat = sections.enumerated().map { arg -> [(section:Int,row:Int)] in
                      return arg.element.data.indices.map{ i in
                          (section:arg.offset,row:i)
                      }
                  }.flatMap{$0}.reduce(0){
                      return $0 + self.tableView(self, heightForRowAt: .init(row: $1.row, section: $1.section))
                  }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-02-01
            • 2022-01-09
            • 2011-08-26
            • 1970-01-01
            • 2015-02-01
            • 1970-01-01
            • 2019-06-04
            相关资源
            最近更新 更多