如果您使用的故事板:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UITableViewCell *cell = sender;
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
{
// Get reference to the destination view controller
YourViewController *vc = [segue destinationViewController];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
// Pass any objects to the view controller here, like...
[vc setIndexPath:indexPath];
}
}
其他
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
YourViewController *vc = [[YourViewController alloc] initWithNibName:@"YourViewController" bundle:nil];
[vc setIndexPath:indexPath];
}
}
我在使用storyboard:我必須調用prepareForSegue方法嗎?我想在didSelectRowAtIndexPath – Wildchild89
而且..我在initWithNibName中調用的UIViewController不是一個TableViewControll,所以它沒有setIndexPath選擇器。 – Wildchild89
如果您從故事板中的tableViewCell中拖動segue,您不需要調用prepareForSegue你的viewController,你必須將它命名爲「YOUR_SEGUE_NAME_HERE」,然後當用戶點擊單元格時,調用prepareForSegue方法。如果你想傳遞數據,你需要在你的UIViewController中有一些@property,例如NSIndexPath或NSInteger行,當你傳遞set屬性[vc setRow:indexPath.row]; – mientus