首页 > XCode中用xib文件自定义的Table Cell,如何实现点击切换View Controller

XCode中用xib文件自定义的Table Cell,如何实现点击切换View Controller

利用xib文件自定义一个Table Cell,如何实现点击该Cell切换View Controller?

因为是使用xib文件自定义Table Cell,所以无法在storyboard中通过拖拽建立segue来实现View Controller的切换。

现在知道Table Cell有个方法TableView:didSelectRowAtIndexPath方法在点击cell后执行。但是不清楚如何在不使用segue的情况下具体实现一个View Controller的Push或者Modal。


楼上的方案是可行的


在那个tableView的delegate里实现TableView:didSelectRowAtIndexPath, 在这个方法里可以初始化想要的push的view controller:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%@", self.data[indexPath.row]);
    // 初始化view controller, 可能需要使用到当前行的数据
    UIViewController *nextViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewControllerIdentifier"];
    // 如果是要push到navigation controller里的话
    [self.navigationController pushViewController:nextViewController animated:YES];
    // 如果是要使用modal view的话
    [self presentViewController:nextViewController animated:YES completion:^{
        // complete
    }];
}

楼上这个答案是可行的,但是符合题意的答案应该是在storyboard里面建立一个prototype cell,然后就可以把prototype cell拖到下一个VC上了,效果等同于上面的代码

【热门文章】
【热门文章】