我有一个正在使用的桌面视图
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath        *)indexPath
{
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}我有一个NSArray *选择的折扣,我已经这样分配了
selectedDiscounts = [self.tableView indexPathsForSelectedRows];我必须将选定的表行数据传递给另一个控制器,在该控制器中,我将使用选定的行填充tableView。
问题是selectedDiscounts要么只保存选定的indexPaths,而不保存数据?因此,它显示了所选对象的数量,但没有显示这些选定单元格的数据。
我希望将选定的行数据存储到NSArray变量中。这有可能吗?谢谢你们。
发布于 2014-08-27 16:57:58
您需要遍历所有索引路径并自己获取数据。
NSMutableArray *array = [[NSMutableArray alloc] init];
for (NSIndexPath *indexPath in selectedDiscounts) {
    // Assuming self.data is an array of your data
    [array addObject: self.data[indexPath.row]];
}现在,您有了包含数据的NSArray,可以传递给下一个控制器。
发布于 2014-08-27 16:55:36
您的selectedDiscounts数组显然是由UITableView方法indexPathForSelectedRows填充的。要存储所选行的实际数据,首先需要建立数组allDiscounts,使用该数组填充第一个表视图。然后,当您显示来自allDiscounts的所有对象并希望选择一些并存储数据时,请执行以下操作:
 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
      [selectedDiscounts addObject:[allDiscounts objectAtIndex:indexPath.row]];
 }发布于 2014-08-27 17:00:48
处理这个问题的方法是在要传递数据的视图控制器上创建一个自定义初始化器方法。就像这样:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   NSArray *selectedDiscounts = yourDataSource[indexPath.row];
   NewViewController *newVC = [[NewViewController alloc] initWithSelectedDiscounts:selectedDiscounts];
   self.navigationController pushViewController:newVC animated:YES];
}另一种方法是在第二个视图控制器(即要传递的数组/字典)上创建一个属性,当它们选择行时,获取该行的信息,并在推送/呈现它之前将其设置在视图控制器上。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   NSArray *selectedDiscounts = yourDataSource[indexPath.row];
   NewViewController *newVC = [[NewViewController alloc] initWith...// whatever you use for the initializer can go here...
   newVC.discounts = selectedDiscounts;
   self.navigationController pushViewController:newVC animated:YES];
}https://stackoverflow.com/questions/25532763
复制相似问题