swift - TableView heightForRowAtIndexPath 返回不正确的部分

标签 swift swift2 tableview

我有一个包含 16 个部分的静态 TableView 。当我尝试修改 heightForRowAtIndexPath 中的高度时,indexPath 的“部分”返回一个从 0 到 3 的数字,而不是预期的 0 到 15。下面是我的代码:

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

    print("Section: \(indexPath.section), Row: \(indexPath.row)")
    if indexPath.row == 0 {
        return heightArray![indexPath.section]
    } else {
        return UITableViewAutomaticDimension
    }

}

它正在打印以下内容:

Section: 0, Row: 0
Section: 0, Row: 1
Section: 0, Row: 2
Section: 1, Row: 0
Section: 1, Row: 1
Section: 2, Row: 0
Section: 2, Row: 1
Section: 2, Row: 2
Section: 3, Row: 0
Section: 3, Row: 1
Section: 0, Row: 0
Section: 0, Row: 1
Section: 0, Row: 2
Section: 1, Row: 0
Section: 1, Row: 1
Section: 2, Row: 0
Section: 2, Row: 1
Section: 2, Row: 2
Section: 3, Row: 0

这是怎么回事?

编辑

这是我上下滚动时发生的事情...

Screen recording of simulator and console

Storyboard截图(这里显然有 16 个部分): storyboard

编辑

这是显示不正确的内容。这是第 4 节。它没有格式化我期望它在 heightForRowAtIndexPath 中的格式。请注意,M03 的格式正确(该部分的单元格 #0 是正确的高度)。 M04 及之后的格式不正确。我在 viewDidAppear 的“heightArray”中设置值,并试图在 tableView.reloadData() 中使用这些高度。

编辑 这是我的 ViewDidAppear 方法:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    self.canDisplayBannerAds = true



    textViewArray = [m1DF,m2DF,m3DF,m4DF,m5DF,m6DF,m7DF,m8DF,m9DF,m10DF,m11DF,m12DF,m13DF,m14DF,m15DF,m16DF]
    print("*****1*****\n\n\n\n\n \(m1DF.bounds.height), \(m1DF.frame.height)")

    for (index, tv) in textViewArray!.enumerate() {

        let fixedWidth = tv.frame.size.width
        tv.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
        let newSize = tv.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
        var newFrame = tv.frame
        newFrame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
        tv.frame = newFrame;
        heightArray[index] = newSize.height
        print("Row \(index) has height \(newSize.height)")


    }

    print(heightArray)


    for (index, _) in (heightArray.enumerate()) {
        heightArray[index] = textViewArray![index].frame.height

    }

    print(heightArray)

    tableView.reloadData()

}

最佳答案

此方法必须返回 16:

func numberOfSections(in tableView: UITableView) -> Int {
        return someValue // as you note you have 16 sections, this number has to have 16 in its value
    }

关于swift - TableView heightForRowAtIndexPath 返回不正确的部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39323544/

相关文章:

iOS:在 numberOfRowsInSection 中返回 2 个计数值

ios - 无法通过 segue 传递变量(Swift)

ios - Tableview 不在 swift 2 中显示数据

types - 如何将 Swift 类型作为方法参数传递?

Swift 2 - UnsafeMutablePointer<Void> 对象

swift - 在 VC1 中从 firebase 检索用户数据并将其发送到 VC2 中的 TableView 以显示时出现问题

java - 将可编辑的 tableView 链接到 javaFX 中的数据库

ios - 在 UITabBar 中启动 Storyboard View

ios - 何时在 Swift 中使用可选值 (?) 以及何时使用关联值 (!)?

ios - UITableView 没有成员 'dequeueReusableCell'