表格视图控制器的多个部分

我有一个小的应用程序,使用多个部分布局的初始表视图。 一个部分显示来自Twitter的最新趋势,另一个部分显示来自Twitter的最新报道。 当我点击趋势列表中的一个项目时,我转换到一个新的表格视图控制器,该控制器显示关于该趋势的最近的推文。 在故事部分的根控制器中,我可以在包含图像,链接等的不同视图控制器中显示更多信息。 问题是,当我selectstory部分中的任何内容时,我被推送到为趋势部分设置的表视图控制器。 我已经命名了每个segue,并为我想要转换的两个视图定制类,并且正在执行此操作以检查哪个segue被调用:

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if([[segue identifier] isEqualToString:@"viewTrendsSearch"]) { //get the controller that we are going to segue to SearchTrendResultsViewController *strvc = [segue destinationViewController]; //get the path of the row that we want from the table view NSIndexPath *path = [self.tableView indexPathForSelectedRow]; //here we get the trend object from the array we set up earlier to hold all trends Trends *results = [currentTrends objectAtIndex:[path row]]; //pass the object that was selected in the table view to the destination view [strvc setQuery: results]; } if([[segue identifier] isEqualToString:@"storyfullDetails"]) { StoriesViewController *svc = [segue destinationViewController]; NSIndexPath *path = [self.tableView indexPathForSelectedRow]; Stories *results = [currentStories objectAtIndex:[path row]]; [svc setStory:results]; } } 

有关如何获得不同意见的build议?

在你的问题中没有足够的信息可以肯定,但是这听起来像是我所说的自动与手动的关联以及每个关于限制的问题。

通过从(原型)表格单元格或其他控件中拖动,在IB中创build自动分段。 关于它的好处是,它是自动的 – 轻敲控件执行segue,并且您需要在代码中执行prepareForSegue:sender:以便目标视图控制器获取正确的数据。 缺点是任何给定的控件(包括原型表单元格)只能有一个传出的继续(否则,故事板不知道自动执行哪个)。

通过从源视图控制器拖动,在IB中创build一个手动的 segue。 这样做的好处是视图控制器可以有多个传出段。 另一方面,它们不与可控制的控件相关联,所以你必须实现逻辑来确定在执行什么(以及执行什么方法)时执行的逻辑。

鉴于这些折衷,有两个可能的解决scheme来解决你的问题:

  1. 使用多个原型表格单元 – 然后每个都可以有自己的传出自动search。 您需要更改您的表视图控制器的tableView:cellForRowAtIndexPath:检查索引path的节号,并为dequeueReusableCellWithIdentifier:select适当的标识符,但是如果趋势和故事单元格有不同的内容,这可能会使事情更加方便或高效。

  2. 使用手动段落。 然后,您的表视图控制器可以实现tableView:didSelectRowAtIndexPath:调用performSegueWithIdentifier:与基于索引path的部分select适当的标识符。

无论哪种方式,您的prepareForSegue:sender:实施看起来不错。