0

所以我想做的是我有一个 NSMutableArray 的数据需要传递给另一个 UITableViewController。这个 NSMutableArray 是一个 NSDictionaries 数组,其中包含我希望在每个表格视图单元格的标题中显示的信息。这是我在继续之前的代码。

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSIndexPath* indexPath = [self.tableView indexPathForCell:sender];

    if ([segue.identifier isEqualToString:@"Title Query"]) {

        UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:indexPath];
        NSString* cellText = cell.textLabel.text;
        NSMutableArray* photosToBeShown = [self titleQuery:cellText];

          if ([segue.destinationViewController respondsToSelector:@selector(setPhotoTitles:)]) {
              [segue.destinationViewController performSelector:@selector(setPhotoTitles:) withObject: photosToBeShown];
              NSLog(@"%@", photosToBeShown);
          }      
    }

}

由 performSelector: withObject: 调用的方法 setPhotoTitles: 是 UITableViewController 上的属性 (NSMutableArray* ) photoTitles 的设置器,因为我想收集数组,所以我可以调用下面的方法来设置我的表格视图单元格的标题。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Photo Title Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.textLabel.text = [self titleForRow:indexPath.row];

    return cell;
}
- (NSString *) titleForRow: (NSUInteger) row
{
    return self.photoTitles[row];
}

当我运行这段代码时会发生什么,我最终进入了一个无限循环,调用了我的 setter 方法 (setPhotoTitles:)。现在我的问题是解决这个问题的正确概念方法是什么,或者我如何才能以这种方式实现它而不会陷入无限循环。我的数组中有我需要的所有信息,但我需要将数组传递给新控制器,而且还能够使用 UITableViewCell 方法来设置行标题。

4

1 回答 1

1

prepareForSegue:方法中,而不是覆盖setPhotoTitles:,您应该在目标视图控制器中创建一个NSArray属性,将 photoTitles 数组传递给NSArray目标视图控制器的属性。所以你的prepareForSegue方法看起来像这样:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSIndexPath* indexPath = [self.tableView indexPathForCell:sender];

    if ([segue.identifier isEqualToString:@"Title Query"]) {

        UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:indexPath];
        NSString* cellText = cell.textLabel.text;
        NSMutableArray* photosToBeShown = [self titleQuery:cellText];

        YourCustomViewController *customViewController = segue.destinationViewController;
        customViewController.photosArrayProperty = photosToBeShown;
    }

}
于 2013-09-11T06:54:47.560 回答