调用heightForRowAtIndexPath,但高度不会更改

时间:2015-12-14 12:49:52

标签: ios swift tableview didselectrowatindexpath heightforrowatindexpath

我有一个名为myTableView的UITableView。我想要tableView做的事实上非常简单。如果用户点击一个单元格,它应该展开,换句话说就是tableView的手风琴。这部分对我来说很好,但我也希望Cell在用户点击mapView上的标记时展开(我正在使用Mapbox)。标记的标题以数字结尾,我用这个数字来确定哪个单元格应该展开。因此,我从标题String中获取数字并创建一个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öheon= height)。

这是我点击第二个单元格时的日志,这个工作正常,单元格按照我的意图扩展,即使是高度读取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

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

2 个答案:

答案 0 :(得分:1)

非常感谢所有试图帮助我解决这个问题的人!我尝试了一切,但我无法使它工作。在研究时,我发现一种方法比我在此处发布的代码更适合我想要的方法。这就是我现在正在做的事情: https://www.youtube.com/watch?v=VWgr_wNtGPMTerminating app due to uncaught exception 'NSRangeException', reason: 'Cannot remove an observer - ios

的帮助下

答案 1 :(得分:0)

您必须编写如下函数:

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