2011-12-30 19 views
8

Có thể chọn vị trí hàng đã chọn trong chế độ xem bảng được phân đoạn trong phương thức prepareForSegue không?iOS: Nhận vị trí hàng trong một TableView được phân đoạn

Đây là mã của tôi cho 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]; 

} 
} 

Tôi nhìn lên một số phương pháp để acces vị trí trực tiếp, nhưng tôi đã không tìm thấy một. Tôi nghĩ rằng tôi đã giám sát một cái gì đó. Có ai biết cách nào không?

+0

tôi không chắc chắn tôi hiểu .. 'selectIndex' trong mã của bạn là vị trí hàng của hàng đã chọn? –

Trả lời

23

Để có được indexPath chọn:

NSIndexPath *selectedRowIndexPath = [self.tableview indexPathForSelectedRow]; 

Để có được hàng đã chọn

NSUInteger selectedRow = selectedIndexPath.row; 

Để có được phần chọn

NSUInteger selectedSection = selectedIndexPath.section; 

Để có được ô đã chọn:

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

cảm ơn bạn rất nhiều –

+1

Tại sao không có điều này làm việc cho tôi bên trong 'preparForSegue'? – Segev

+0

Để nhận hàng nhanh chóng, hãy sử dụng 'self.tableView.indexPathForSelectedRow() ?. row)' – User

0

Trong nhanh chóng 2.2:

Lấy thẻ của di động của bạn vào các đại biểu 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 
    } 

Và sau đó caputure nó vào prepareForSegue trought người gửi:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if segue.identifier == "yourSegue" { 
     let vc = segue.destinationViewController as! destinationControllerClass 
     vc.passedIndex = sender?.tag 
    } 
} 
Các vấn đề liên quan