gpt4 book ai didi

ios - 通过performSelector将数据从一个UITableViewController转移到另一个UITableViewController :withObject:

转载 作者:行者123 更新时间:2023-11-29 03:39:00 24 4
gpt4 key购买 nike

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

- (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);
}
}

}

方法setPhotoTitles:由performSelector: withObject:调用,它是UITableViewController上属性(NSMutableArray*) photoTitles的setter,我正在继续使用它,因为我想收集数组,所以我可以调用下面的方法设置我的表格 View 单元格的标题。

- (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:)。现在我的问题是解决这个问题的正确概念方法是什么,或者我如何才能以这种方式实现它而不会陷入无限循环。我的数组中有我需要的所有信息,但我需要将数组传递给新 Controller ,而且还能够使用 UITableViewCell 方法来设置行标题。

最佳答案

prepareForSegue: 方法中,而不是覆盖 setPhotoTitles:,您应该在目标 View Controller 中创建一个 NSArray 属性,作为传递photoTitles 数组到目标 View Controller 的 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;
}

}

关于ios - 通过performSelector将数据从一个UITableViewController转移到另一个UITableViewController :withObject:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18731027/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com