我有一个名为 myTableView 的 UITableView。我想让 tableView 做的事情其实很简单。如果用户点击一个单元格,它应该展开,换句话说,tableView 是手风琴。这部分对我来说很好,但我也希望当用户点击 mapView 上的标记时单元格展开(我正在使用 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
提前感谢,对不起我的英语不好!