NSIndexPath 以 1 开头

NSIndexPath starts with 1

我有以下问题:

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 方法。 (我想是因为这个标签上有不止一个观察者。-> 我在这个单元格上有委托方法,另一个我想是表视图。)

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)")
}

如果我点击单元格而不是标签或图像,一切正常,我得到正确的行数。例如,也许有人知道我如何在标签上添加多个 "observer"。这样我的 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!()
}