2015-09-02 40 views
1

开始,我有以下问题:NSIndexPath 1

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    // ... 
    println(indexPath.row) 
} 

我的输出是这样的:

1 
0 
1 
0 

的numberOfRowsInSection告诉我,我有项目:

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方法不会被调用。 (我想是因为这个标签上不止一个观察者 - >我已经在这个小区,另一个我想是的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都知道在单元格中点击的标签。我认为这可以解决我的问题,我可以用正确的行知道我自己的Selectormethod。

对于谁想要知道我的意思与Selectormethod人:

let gestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("labelPressed")) <- 
label.addGestureRecognizer(gestureRecognizer) 

func labelPressed() { 
    delegate?.switchToOwnProfil!() 
} 

的神秘之处,第一输出显示我先1也不是0,但也许我忽略了一些东西。

在此先感谢!

+0

你可以简单地标记与indexpath.row您的标签在cellForAtIndexPath –

+0

后,在你的方法获取标签的标签,你会得到的tableView –

+0

的行你能显示一些“标记”标签的代码片段? –

回答

1
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!() 
} 
+0

如果语法错误,但想法相同,请原谅我。 –