天天看點

怎樣确定點選的是哪一個cell上的按鈕

在iOS的開發中,我們經常有這樣的需求,點選表格中某一個cell上的編輯按鈕,跳轉到對應的編輯界面,比如表格顯示的是你的多個收貨位址(如下圖所示),這就需要确定點選的是哪一個cell上的按鈕,這樣才能把對應的模型資料傳遞給下一個頁面,作為資料源.最下面是我跟人感覺比較好用的方法,簡單粗暴,而且有效.

怎樣确定點選的是哪一個cell上的按鈕
// cell上'edit按鈕'的點選事件
- (IBAction)editClick:(id)sender {
    
    // create toVC
    AddressEditTableController *toVC = [[AddressEditTableController alloc] initWithStyle:UITableViewStyleGrouped];
    // 擷取'edit按鈕'所在的cell
    UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview];
    // 擷取cell的indexPath
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    // 列印 --- test
    NSLog(@"點選的是第%zd行",indexPath.row + 1);
    
    // 跳轉
    [self.navigationController pushViewController:toVC animated:YES];
}