2011-12-30 37 views
8

是否可以在prepareForSegue方法中的分段表格视图中访问选定的行位置?iOS:在分段表格中获取行位置查看

这是我对Segue公司代码:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 

if ([[segue identifier] isEqualToString:@"CurrentEvent"]) { 

    Tab1_DetailTableViewController *vc = [segue destinationViewController]; 

    // get the selected index 
    NSInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row]; 

} 
} 

我抬头几种方法来直接存取权限的位置,但我没有找到一个。我想我已经监督了一些事情。有人知道一种方式吗?

+0

我不知道我明白了..代码中的'selectedIndex'是所选行的行位置? – 2011-12-30 23:14:30

回答

23

获取所选indexpath:

NSIndexPath *selectedRowIndexPath = [self.tableview indexPathForSelectedRow]; 

要获得所选行

NSUInteger selectedRow = selectedIndexPath.row; 

获取所选部分

NSUInteger selectedSection = selectedIndexPath.section; 

获取所选单元格:

UITableViewCell *selectedCell = [self.tableview cellForRowAtIndexPath:selectedRowIndexPath]; 
+1

非常感谢你 – 2011-12-31 09:39:40

+1

为什么这些在'prepareForSegue'里面都不适合我? – Segev 2012-12-19 14:39:20

+0

要在swift中获得行,请使用'self.tableView.indexPathForSelectedRow()?. row)' – User 2014-09-26 18:06:38

0

在SWIFT 2.2:

让你的电池的标签进入的cellForRowAtIndexPath委托:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! taskCell 
     cell.tag = indexPath.row    
     return cell 
    } 

然后caputure入prepareForSegue trought发件人:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if segue.identifier == "yourSegue" { 
     let vc = segue.destinationViewController as! destinationControllerClass 
     vc.passedIndex = sender?.tag 
    } 
}