2014-10-09 18 views
25
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
    { 
    if ([segue.identifier isEqualToString:@"Action"]) 
    { 
     NSIndexPath *indexPath = [self.tbl indexPathForSelectedRow]; 
     SecondViewController *destViewController = segue.destinationViewController; 
     destViewController.getString = [getArray objectAtIndex:indexPath.row]; 
    } 
} 

tôi muốn truy cập hàng đã chọn index, nhưng hiển thị null cho mỗi hàng đã chọn. làm ơn giúp tôi?Cách lấy đường dẫn chỉ mục trong PrepareForSegue

+0

Xin vui lòng kiểm tra self.tbl! = Nil – Matz

+0

Bạn có không thực hiện kết nối của biến tbl để tableview vào storyboard. – Rajesh

Trả lời

25

Swift 3.0/iOS 10

tableView.indexPathForSelectedRow đã được giới thiệu trong iOS 9

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if let indexPath = tableView.indexPathForSelectedRow{ 
     let selectedRow = indexPath.row 
     let detailVC = segue.destination as! ParkDetailTableVC 
     detailVC.park = self.parksArray[selectedRow] 
    } 
} 
+0

Cũng lưu ý là tài sản song song trên quan điểm bộ sưu tập, 'indexPathsForSelectedItems', mà có thể được sử dụng như sau: ' '' nếu chúng ta hãy selectedIndexPathRow = collectionView .indexPathsForSelectedItems .first .row { doStuff (với:?? MyDataSourceArr [selectedIndexPathRow] } '' ' – AmitaiB

+0

Giải pháp đơn giản này đã lưu ngày của tôi :) – iPhoneDeveloper

+0

Cảm ơn bạn! Điều này đã cứu tôi! –

0

xin lỗi, tôi không hiểu bạn muốn làm gì. nó có thể có được indexPath.row thông qua phương pháp này UITAbleViewDelegate rằng nó được gọi là khi bạn chạm vào tế bào của tableView của bạn:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    self.myVariable = indexPath.row 
} 

và sau đó bạn có thể truy cập đến giá trị này trong prepareForSegue theo cách này:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ 
     //do something with self.myVariable 
} 

Tôi hy vọng tôi đã giúp bạn.

+0

Điều đó không nhất thiết đúng nếu chạm vào ô đó sẽ làm tăng Phân đoạn của bạn. Sẽ không làm việc. – Spacemonkey

63

Hai trường hợp:

1) Segue kết nối từ viewController

Gọi segue từ phương pháp didSelectRowAtIndexPath của bạn, vượt qua indexPath như sender

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [self performSegueWithIdentifier:@"Action" sender:indexPath]; 
} 

Sau đó, bạn có thể nhận được indexPath như người gửi trong prepareForSegue:sender: phương pháp

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString:@"Action"]) 
     { 
      NSIndexPath *indexPath = (NSIndexPath *)sender; 
      SecondViewController *destViewController = segue.destinationViewController; 
      destViewController.getString = [getArray objectAtIndex:indexPath.row]; 
     } 
    } 

2) segue kết nối từ các tế bào

Không cần phải thực hiện didSelectRowAtIndexPath phương pháp và performSegueWithIdentifier: .Bạn có thể trực tiếp lấy sender như UITableviewCell trong prepareForSegue:sender: phương pháp.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString:@"Action"]) 
    { 
     NSIndexPath *indexPath = [self.tableView indexPathForCell:sender]; 
     SecondViewController *destViewController = segue.destinationViewController; 
     destViewController.getString = [getArray objectAtIndex:indexPath.row]; 
    } 
} 
+1

@PintuRajput, bạn không nên làm theo cách này nếu bạn đã kết nối segue trực tiếp từ ô. Trong trường hợp đó, đối số người gửi sẽ là ô và bạn có thể sử dụng phương thức tableView indexPathForCell: để lấy indexPath từ người gửi. Nếu segue của bạn là từ các tế bào, bạn không nên thực hiện didSelectRowAtIndexPath, hoặc gọi performSegue. – rdelmar

+0

Thats true, trong trường hợp của tôi là segue từ ViewController. –

+0

Cảm ơn thông tin! – Tommy

3

Như thế này

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UITableViewCell *)sender { 
    FTGDetailVC *detailVC = (id)segue.destinationViewController; 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:sender]; 

    FTGNote *note = self.notes[indexPath.row]; 
    [detailVC updateWithNote:note]; 
} 
Các vấn đề liên quan