ios - NSIndexPath 以 1 开头

标签 ios swift tableview

我有以下问题:

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/

相关文章:

ios - UITableViewCell 中的 UIButton 不会在点击时突出显示

objective-c - 将核心数据功能添加到现有 iOS 应用程序时遇到问题

swift - UICollectionView 单元格 View 重叠

当应用程序在后台时,iOS 12.4 AVAudioRecorder.record 返回 false

ios - UITableViewCell Size 在运行时没有扩展。?

Xamarin Forms-TableView - 转到第一个元素

ios - 这段 Swift w/SpriteKit 代码没有达到预期目的

ios - 当用户收到超过 1 个推送通知时,应用程序图标上的角标(Badge)编号不会增加

objective-c - Xcode 更新后应用程序崩溃

ios - Swift:表格 View 单元格上的渐变