我有以下问题:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// ...
println(indexPath.row)
}
我的输出是这样的:
1
0
1
0
numberOfRowsInSection 告诉我我有 10 个项目:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (self.actionDto != nil){
println(self.actionDto?.count)
return self.actionDto!.count
}
return 0
}
我检查过这个indexpath.row is starting from 1 instead of 0 ?但无法真正理解答案或解决我的问题。
实际上我只是想点击单元格中的标签并做一些其他事情。但我必须确切知道它在哪一行。
我考虑过使用 didSelectRowAtIndexPath 方法。然后我遇到问题,如果我点击标签,则不会调用 didSelectRowAtIndexPath 方法。 (我想是因为这个标签上有不止一个观察者。-> 我在这个单元格上有委托(delegate)方法,另一个我想是 TableView 。)
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let indexPath = tableView.indexPathForSelectedRow()
let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as! TimelineCell
println("The number of cell is \(currentCell.numberOfRowAtIndexPath)")
}
如果我点击单元格而不是标签或图像,一切正常,我得到正确的行数。例如,也许有人知道我如何在标签上添加多个“观察者”。这样我的 Selectormethod 和 didSelectRowAtIndexPath 都知道在单元格中点击的标签。我认为这可以解决我的问题,我可以在了解正确行的情况下执行自己的 Selector 方法。
对于想知道我所说的 Selectormethod 是什么意思的人:
let gestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("labelPressed")) <-
label.addGestureRecognizer(gestureRecognizer)
func labelPressed() {
delegate?.switchToOwnProfil!()
}
奇怪的是,第一个输出首先显示 1 而不是 0,但也许我忽略了什么。
提前致谢!
最佳答案
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// ...
yourLabel.tag = indexPath.row
let gestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("labelPressed:")) <-
yourLabel.addGestureRecognizer(gestureRecognizer)
println(indexPath.row)
}
在你的函数中
func labelPressed(label:UILable) {
println(label.tag)
delegate?.switchToOwnProfil!()
}
关于ios - NSIndexPath 以 1 开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32357013/