Ios 通过PerformSelect:withObject将数据从一个UITableViewController分离到另一个UITableViewController:
所以我要做的是我有一个NSMutableArray的数据,我需要传递给另一个UITableViewController。此NSMutableArray是一个NSDictionary数组,其中包含我希望在每个表视图单元格的标题中显示的信息。这是我的密码Ios 通过PerformSelect:withObject将数据从一个UITableViewController分离到另一个UITableViewController:,ios,uitableview,segue,uistoryboardsegue,Ios,Uitableview,Segue,Uistoryboardsegue,所以我要做的是我有一个NSMutableArray的数据,我需要传递给另一个UITableViewController。此NSMutableArray是一个NSDictionary数组,其中包含我希望在每个表视图单元格的标题中显示的信息。这是我的密码 - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { NSIndexPath* indexPath = [self.tableView indexPa
- (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方法设置行标题 在
prepareforsgue:
方法中,您应该在目标视图控制器中创建一个NSArray
属性,而不是覆盖setPhotoTitles:
,将photoTitles数组传递给目标视图控制器的NSArray
属性。因此,您的prepareforsgue
方法如下所示:
- (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;
}
}
你是否覆盖了照片标题的设置器?是的,我覆盖了。我现在意识到这是造成我的问题的原因,但是为什么你不能重写setter?你应该能够--你在那个方法中放了什么?我在修复我的问题时删除了它,它只是一个懒惰的实例化,我认为这是有意义的,但函数只是不断被调用,而且每次都有效调用时,它会向属性中添加另一个数组,而不是在当前属性中擦除该数组并初始化一个新数组。所以我的属性只是在继续增长。不管怎样,这是我的一个错误,因为我使用的是可变数组。当我真的需要实例化一个数组的新实例时,我的数组会继续增长。谢谢你的帮助