Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS:我正在尝试将所选行(数据)发送到另一个控制器。_Ios_Objective C_Uitableview - Fatal编程技术网

iOS:我正在尝试将所选行(数据)发送到另一个控制器。

iOS:我正在尝试将所选行(数据)发送到另一个控制器。,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个我正在使用的tableview - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; } 我有一个NSArray*selectedDiscounts,我

我有一个我正在使用的tableview

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath        *)indexPath
{
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}
我有一个NSArray*selectedDiscounts,我这样分配的

selectedDiscounts = [self.tableView indexPathsForSelectedRows];
我必须将所选表行数据传递给另一个控制器,在那里我将用所选行填充tableView

问题是selectedDiscounts是否只保存选定的IndExpath而不保存数据?因此,它会显示选定对象的数量,但不会显示这些选定单元格的数据

我想将所选行数据存储到NSArray变量中。可能吗?谢谢大家。

您的selectedDiscounts数组显然已填充了UITableView方法indexPathForSelectedRows。要存储所选行的实际数据,首先需要建立一个数组AllDiscounters,用它填充第一个表视图。然后,当显示所有折扣中的所有对象并希望选择一些对象并存储数据时,请执行以下操作:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
      [selectedDiscounts addObject:[allDiscounts objectAtIndex:indexPath.row]];
 }

您需要遍历所有索引路径并自己获取数据

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,其中包含可以传递给下一个控制器的数据

处理这个问题的方法是在要将数据传递给的视图控制器上创建一个自定义初始值设定项方法。大概是这样的:

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

你确定一开始就正确填充了所有折扣吗?是的,我非常确定我的allDiscount NSArray对象上挂着所有折扣值。因此,我不明白为什么在正确填充数组时它显示为零。它应该可以正常工作我的allDiscount显示了所有折扣的列表,我不确定为什么不起作用,可能是因为每次我选择一行时,都会调用DidSelectRowatineXpath,不确定。但是谢谢你的帮助和建议