本文介绍了对UITableView使用didSelectRowAtIndexPath或prepareForSegue方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在使用故事板,我有一个UITableView。我有一个segue设置,从我的表推送到详细VC。但是我应该使用哪种方法来处理这个问题?我必须将几个对象传递给详细视图。但我是否使用 didSelectRowAtIndex 或 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender ? 解决方案如果你使用 prepareForSegue:sender:那么你就不会如果你以后决定从表格视图之外的某些控件触发segue,那就要改变了。 prepareForSegue:sender:消息被发送到当前视图控制器,所以我建议这样的事情: - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { //假设self.view是表视图 NSIndexPath * path = [self.tableView indexPathForSelectedRow]; DetailObject * detail = [self detailForIndexPath:path]; [segue.destinationViewController setDetail:detail]; } 在Swift中: 覆盖func prepareForSegue(segue:UIStoryboardSegue,sender:AnyObject?){ let path = self.tableView.indexPathForSelectedRow()! segue.destinationViewController.detail = self.detailForIndexPath(path)} I'm using storyboards and I have a UITableView. I have a segue setup that pushes from my table to the detail VC. But which method should I use to handle this? I'll have to pass a couple objects to the detail view. But do I use didSelectRowAtIndex or -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender? 解决方案 If you use prepareForSegue:sender:then you won't have as much to change if you later decide to trigger the segue from some control outside the table view.The prepareForSegue:sender: message is sent to the current view controller, so I'd suggest something like this:- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ // Assume self.view is the table view NSIndexPath *path = [self.tableView indexPathForSelectedRow]; DetailObject *detail = [self detailForIndexPath:path]; [segue.destinationViewController setDetail:detail];}In Swift:override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { let path = self.tableView.indexPathForSelectedRow()! segue.destinationViewController.detail = self.detailForIndexPath(path)} 这篇关于对UITableView使用didSelectRowAtIndexPath或prepareForSegue方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-24 18:26