2016-03-08 21 views
1

的情況下有沒有辦法讓在其中選擇一排段的實例?有可能獲得該部分的索引,所選單元格的索引,所選單元格的實例......但本節的實例?UITableView的didSelectRowAtIndexPath方法獲得選擇部分

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    let indexPath = tableView.indexPathForSelectedRow // index path of selected cell 

    let headerCellIndex = indexPath!.section // index of selected section 
    let headerCellName = ????? // instance of selected section 

    let cellIndex = indexPath!.row // index of selected cell 
    let cellName = tableView.cellForRowAtIndexPath(indexPath!) // instance of selected cell 
} 

謝謝。

+0

如何您插入節時的tableview負載的名字嗎? – Lee

+0

'字符串(內容[段] .sectionName)' 這也將在'didSelectRowAtIndexPath'-method..But工作是不是有辦法讓節的名稱在某種程度上類似於你怎麼弄的名稱選中的單元格? – manu

+0

是數組/對象的內容嗎? – Lee

回答

5

這總是工作很適合我。我總是將它打開以及選擇分配給它的班級,以確保我擁有正確類型的單元格。在這個例子中,我有MyTableViewCell,但它當然可以是任何東西。

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    if let cell = tableView.cellForRowAtIndexPath(indexPath) as? MyTableViewCell { 

    } 
} 
0

或雨燕3.0,使用

覆蓋FUNC的tableView(_的tableView:UITableView的,didSelectRowAt indexPath:IndexPath){

//你的代碼...

}

0

隨着功能DidSelectRows喲你可以使用這樣的開關(例如, 3段,變行)

switch indexPath.section { 
case 0: 
    switch indexPath.row { 
     case 0: 
     Action to do with first cell (ex. performSegue(withIdentifier: "", sender: nil) 
     case 1: 
     Action to do with second cell 
     default: 
     break 
     } 
case 1: 
    Things you want to do in the second section 
case 2: 
    Things you want to do in the third section 
default: 
break 
} 
+0

部分動態地來自web服務,我如何獲得indexpath。 – saravanar

相關問題