S. Birklin
S. Birklin

Reputation: 1385

NSIndexPath starts with 1

I have the following problem:

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

My output goes like this:

1
0
1
0

The numberOfRowsInSection tells me that I've got 10 items:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if (self.actionDto != nil){
        println(self.actionDto?.count)
        return self.actionDto!.count
    }
    return 0
}

I have checked this indexpath.row is starting from 1 instead of 0? but can't really follow the answer or solve my problem.

Actually i just want to tap a Label in the cell and do some other stuff. But i have to know exactly which row it was.

I thought about using didSelectRowAtIndexPath method. Then i have problem that if i tap on the label the didSelectRowAtIndexPath method isn't called. (I think because of more than one observer on this label. -> i have delegate method on this cell and the other one i suppose is the 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)")
}

If i click into the cell but not on labels or images all works fine and i get the right number of row. Perhaps someone knows how i can add more than one "observer" on a label for example. So that my Selectormethod and didSelectRowAtIndexPath both knew about the label tapped in the cell. I think this could solve my problem that i can do my own Selectormethod with the knowledge of the right row.

For people who want to know what i mean with Selectormethod:

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

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

The mysterious thing is, that the first output shows me first 1 and not 0 but perhaps i overlooked something.

Thanks in advance!

Upvotes: 1

Views: 576

Answers (1)

Arslan Asim
Arslan Asim

Reputation: 1302

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

and in your function

func labelPressed(label:UILable) {
        println(label.tag)
   delegate?.switchToOwnProfil!()
}

Upvotes: 1

Related Questions