2014-08-27 49 views
0

我有我使用iOS:我试图将选定的行(数据)发送到另一个控制器。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath  *)indexPath 
{ 
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; 
} 

针对其的tableview我有一个NSArray * selectedDiscounts我已经这样分配

selectedDiscounts = [self.tableView indexPathsForSelectedRows]; 

我有将数据传递所选择的表行到另一个控制器,其中我将用选定的行填充tableView。

问题已选择折扣要么只保存选定的indexPaths而不能保存数据?因为它显示了所选对象的数量,但没有显示所选单元的数据。

我想将选定的行数据存储到NSArray变量中。那可能吗?多谢你们。

回答

0

您需要通过所有的索引路径的迭代和自己获取数据。

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

+0

Berube'谢谢先生,这工作完美。 – Ninja9 2014-08-27 18:12:07

0

您的selectedDiscounts阵列显然正在填充UITableView方法indexPathForSelectedRows。要存储选定行的实际数据,您需要首先建立一个数组allDiscounts,使用该数组填充第一个表视图。然后,当你显示所有从allDiscounts对象,并要选择一些和存储数据做到这一点:

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

试过这个,但我的所有折扣shiws我零。 :( – Ninja9 2014-08-27 17:00:21

+0

你确定你正在首次正确填充'allDiscounts'吗? – angerboy 2014-08-27 17:03:23

+0

是的,我很确定我的所有折扣价值都挂在我的allDiscount NSArray对象上 – Ninja9 2014-08-27 18:01:59

0

我会处理这个问题的方法是在您要传递数据的视图控制器上创建自定义初始化方法。事情是这样的:

- (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]; 
} 
相关问题