2015-09-02 42 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

如果語法錯誤,但想法相同,請原諒我。 –