2012-01-13 82 views
2

我的目的是当view1上的一个按钮,用户点击(去view2),我将所有选中的单元发送从UITableView刚刚签入view1view2。 因此,在将表格传递到下一个视图之前,我想测试我是否处于正确的轨道上,但似乎不是。这是我的代码:让所有选中的单元格值

-(IBAction)goToView2{ 
    //get all section selectioned items into an array 
    themesChosed=[tView indexPathsForSelectedRows];//tView is the outlet of my UITableView and themesChosed is an NSArray declared in the .h file 
    for (int i=0; i<=[themesChosed count]; i++) { 
     NSLog(@"%i",[[themesChosed objectAtIndex:i]integerValue]); 
    } 


} 

这总是返回我一个0

2012-01-13 15:52:06.472 MyApp[876:11603] 0 

虽然我选择了在桌子上的几个项目。

+0

什么的NSLog( @“%d”,themesChosed.count);显示?这是否与tableview中选定项目的数量相同? – 2012-01-13 16:50:34

回答

5

themesChosed中的对象是NSIndexPath对象。你要访问像这样的rowsection属性:

-(IBAction)goToView2{ 
    //get all section selectioned items into an array 
    themesChosed=[tView indexPathsForSelectedRows];//tView is the outlet of my UITableView and themesChosed is an NSArray declared in the .h file 
    for (int i=0; i<=[themesChosed count]; i++) { 
     NSIndexPath *thisPath = [themesChosed objectAtIndex:i]; 
     NSLog(@"row = %i, section = %i", thisPath.row, thisPath.section); 
    } 
} 
+0

嗨,thanx为你的答案,实际上,我选择哪一节,无论我选择多少行,我总是得到0:'row = 0,section = 0 ' – Malloc 2012-01-13 16:36:12

+0

这意味着两件事之一1)你的tView插座没有像你想象的那样挂钩,或者2)没有选择行。 – agilityvision 2012-01-14 02:20:54

2

第一个NSIndexPath不是一个整数,它是一个带有行和节的方法的对象,所以这可能是为什么你得到一个零。你确定你有allowsMultipleSelection设置为YES吗?

0

如果你没有得到指数值,那么最好的办法好办如下访问索引号:

NSArray *indexes = [self.tableView indexPathsForSelectedRows]; 
    for (NSIndexPath *path in indexes) { 
    NSUInteger index = [path indexAtPosition:[path length] - 1]; 
    [selectedOptionsArray addObject:[optionHolderArray objectAtIndex:index]]; 
    }