2012-11-22 12 views
5

Tôi gặp sự cố với nút phụ kiện UItableViewCell. Nút phụ kiện của tôi được "kết nối" với khoảng cách khác với UIViewController khác. Khi tôi nhấn vào một nút phụ kiện, tôi lấy đối tượng từ mô hình và tôi "chuyển" đối tượng này đến đích destinationViewController. Đây là tôi chuẩn bị cho phương pháp segue:Nhận chỉ mụcPath cho nút phụ kiện đã chọn trong UITableView

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString:@"IdentityCard"]) 
    { 
     NSLog(@"%i" ,self.tableView.indexPathForSelectedRow.row); 
     [segue.destinationViewController showIdentityCardForTeacher:[self.teachers getTeacherInPosition:self.tableView.indexPathForSelectedRow.row]]; 
    } 
} 

Vấn đề là các NSLog vào đầu của phương pháp này (sau khi "if") hiển thị lúc nào cũng là số 0! Tôi cố gắng để thực hiện phương pháp này:

-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath 

nhưng chuẩn bị cho phương pháp segue được thực hiện trước khi phương pháp này, vì vậy tôi không thể đặt bất kỳ @property cho "tiết kiệm" các indexPath. Làm cách nào để giải quyết vấn đề này?

Trả lời

1

Tôi khắc phục sự cố theo cách này: Tôi đã kết nối UIViewController với destinationViewController (KHÔNG TỪ BÀI TRUY CẬP BUT TỪ UIViewController).

Tôi tạo ra một @property trong View Controller đầu tiên:

@property (nonatomic) NSInteger indexPathRow; 

sau đó tôi sửa đổi mã như thế này:

-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"Disclosure Tapped----> %i" ,indexPath.row); 
    self.indexPathRow = indexPath.row; 
    [self performSegueWithIdentifier:@"IdentityCard" sender:self]; 
} 

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString:@"IdentityCard"]) 
    { 
     NSLog(@"%i" ,self.tableView.indexPathForSelectedRow.row); 
     [segue.destinationViewController showIdentityCardForTeacher:[self.teachers getTeacherInPosition:self.indexPathRow]]; 
    } 
} 

Bây giờ nó hoạt động!

20

Trong:

- (void) prepareForSegue: (UIStoryboardSegue *) segue người gửi: (id) sender

... bạn có tham số 'gửi' cho phép bạn biết các tế bào của phụ kiện đã được Tapped, do đó, với:

index = [self.tableView indexPathForCell: sender] .row;

Bạn có thể lấy chỉ mục của nó để sử dụng nó.

Sau đó, bạn không cần sử dụng phương thức phụ kiện của TableViewButtonTappedForRowWithIndexPath.

+0

Cảm ơn! Tôi thực sự không bao giờ nhận ra rằng người gửi automagically được dân cư với các tế bào. Nó có ý nghĩa mặc dù: D – Fogmeister

0

@ Giải pháp của Andres có tác dụng với tôi nhưng nếu tôi muốn thêm nút Thêm vào thanh điều hướng và tôi muốn hiển thị cùng một trình điều khiển xem với cùng một mã định danh phân biệt thì mã không hoạt động vì người gửi không phải là phân lớp của UITableViewCell. Vì vậy, đây là giải pháp của tôi với mã @Andres:

- (void) addItem: (id) sender { 
    [self performSegueWithIdentifier: @"Identifier" sender: self]; 
} 

- (void) prepareForSegue: (UIStoryboardSegue *) segue sender: (id) sender { 
    id selectedObject; 
    if (![[sender class] isSubclassOfClass: [UITableViewCell class]]) { 
     // create a new object 
     selectedObject = [...]; 
    } 
    else { 
     selectedObject = [objectArray objectAtIndex: [self.tableView indexPathForCell: sender].row]; 
    } 

    if ([[segue identifier] isEqualToString: @"Identifier"]) { 
     // use the new or existing object 
     [selectedObject doSonething]; 
    } 
}