heightForRowAtIndexPath 被调用,但高度没有改变

heightForRowAtIndexPath is called, but the height doesn't change

我有一个名为 myTableView 的 UITableView。我想让 tableView 做的事情其实很简单。如果用户点击一个单元格,它应该展开,换句话说,tableView 的手风琴。这部分对我来说效果很好,但我也希望当用户点击 mapView 上的标记时 Cell 展开(我使用的是 Mapbox)。标记的标题以数字结尾,我使用这个数字来确定应该扩展哪个单元格。因此,我从标题字符串中获取数字并创建一个 NSIndexPath(称为 localIndexPath),并将该数字作为行。之后我手动选择相应的单元格:

self.myTableView.selectRowAtIndexPath(localIndexPath, animated: true, scrollPosition: UITableViewScrollPosition.None)

然后我通过以下方式调用 didSelectRowAtIndexPath:

self.tableView(self.myTableView, didSelectRowAtIndexPath: localIndexPath)

这然后调用 heightForRowAtIndexPath,这两种方式都成功调用,但实际高度仅在我单击单元格时发生变化,而不是在我单击标记时发生变化。我的问题是为什么会这样,我怎样才能让它双向工作?

所有对应方法的代码如下:

import UIKit

@IBDesignable class PullUpView: UIView, UITableViewDataSource, UITableViewDelegate {


@IBOutlet var tableCell: UIView!
@IBOutlet weak var hideButton: UIButton!
@IBOutlet weak var myTableView: UITableView!

var view:UIView!
var selectedRowIndex = NSIndexPath(forRow: 99, inSection: 0)
var localIndexPath = NSIndexPath(forRow: 99, inSection: 0)
//irrelevant code...

//This is the function I call when I click a marker
func callDidSelectRowAtIndexPathManually(clickedAnnotation: String){
    print("MANUALLY: \(clickedAnnotation)")
    var tourNumber: String = "Default"
    if(clickedAnnotation.characters.count == 18){
        let idx = advance(clickedAnnotation.startIndex, 15)
         tourNumber = String(clickedAnnotation[idx])
      //  print("TOUR NUMMER IST: \(tourNumber)")
     //in case of two digit number
    }else if(clickedAnnotation.characters.count == 19){
        let idx = advance(clickedAnnotation.startIndex, 15)
        let idxTwo = advance(clickedAnnotation.startIndex, 16)
         tourNumber = String(clickedAnnotation[idx]) + String(clickedAnnotation[idxTwo])
       // print("TOUR NUMMER IST: \(tourNumber)")
    }
    let tourNumberAsInt = Int(tourNumber)
    print("TOUR NUMMER IST: \(tourNumberAsInt)")
    if(tourNumberAsInt != nil){
    localIndexPath = NSIndexPath(forRow: tourNumberAsInt!, inSection: 0)
    //The manual selection and call
    self.myTableView.selectRowAtIndexPath(localIndexPath, animated: true, scrollPosition: UITableViewScrollPosition.None)
    self.tableView(self.myTableView, didSelectRowAtIndexPath: localIndexPath)
    getCellHeight(localIndexPath)
    }
}

 //I just store the indexPath in a variable
 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    NSLog("Und gewählter Index ist:  \(indexPath.row)")
    selectedRowIndex = indexPath
    myTableView.beginUpdates()
    print("did start updates")
    myTableView.endUpdates()
    print("did end updates")
    getCellHeight(selectedRowIndex)

}

//I use this function to check the height of the cell
func getCellHeight(indexPath: NSIndexPath){
    var cell = tableView(myTableView, cellForRowAtIndexPath: indexPath)
    print("HÖHE VON: \(indexPath.row) ist jetzt: \(cell.bounds.height)")
}

//I check wheter selectedIndexPath or localIndexPath are equal to the indexPath argument, if it is the cell should expand
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if(selectedRowIndex == indexPath && selectedRowIndex.row == indexPath.row || localIndexPath == indexPath){
        print("heightforrow CALLED für: \(indexPath.row)")
        return 200
   }
     print("heightforrow NOT CALLED für: \(indexPath.row)")
    return 60

}

抱歉,这些印刷品是德文的,但我认为您可以理解它们的意思(ist = is, für = for, höhe von = height of)。

这是我点击第二个单元格时的日志,它工作正常,单元格按我的预期展开,即使高度显示为 44:

did start Updates
heightforrow NOT CALLED für: 0
heightforrow CALLED für: 1
heightforrow NOT CALLED für: 2
did end updates
HÖHE VON: 1 ist jetzt: 44.0

这是我点击注释时的日志,单元格没有在视觉上展开:

did start updates
heightforrow NOT CALLED für: 0
heightforrow NOT CALLED für: 1
heightforrow CALLED für: 2
heightforrow NOT CALLED für: 0
heightforrow NOT CALLED für: 1
heightforrow CALLED für: 2
did end updates
HÖHE VON: 2 ist jetzt: 44.0
HÖHE VON: 2 ist jetzt: 44.0

提前致谢,抱歉我的英语不好!

您必须编写如下函数:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 44.00
}

非常感谢所有试图帮助我解决这个问题的人!!!我尝试了一切,但我无法让它发挥作用。在研究过程中,我发现了一种比我在此处发布的代码更适合我想要的方法。这是我现在正在做的: https://www.youtube.com/watch?v=VWgr_wNtGPM 在以下帮助下: