2013-07-31 31 views
0

我有一个开关的自定义UITableViewCell子类。在子类中的valueChanged操作中,我根据开关状态设置单元的选择的属性。自定义UITableViewCell的手动选择不会使tableView.indexPathsForSelectedRows返回单元格?

self.selected = self.optionSwitch.isOn 

在处理这些细胞中的UITableViewController请问选定行的表视图,但它返回零(不选择行)。

self.tableView.indexPathsForSelectedRows 

为什么它返回nil如果选中单元格?我确定他们是因为我找回了一个来检查选定的属性,实际上它是

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; 
BOOL isSelected = cell.isSelected; 

PS1:我已经把我的表视图的allowsMultipleSelection财产。

PS2:我知道tableView:selectRowAtIndexPath:工作,但我不想为我的自定义单元格创建委托只是为了通知表视图并使tableview选择该行。

回答

0

我放弃了,并在PS2中使用了解决方案。为您的自定义单元格创建一个委托并调用selectRowMethod。

#pragma mark - CellDelegate 

- (void)didSelectCell:(CustomCell *)cell 
{ 
    NSIndexPath *ip = [self.tableView indexPathForCell:cell]; 
    [self.tableView selectRowAtIndexPath:ip animated:NO scrollPosition:UITableViewScrollPositionNone]; 
} 

- (void)didDeselectOptionCell:(CustomCell *)cell 
{ 
    NSIndexPath *ip = [self.tableView indexPathForCell:cell]; 
    [self.tableView deselectRowAtIndexPath:ip animated:NO]; 
} 
0

万一有人有同样的问题,这是我的解决办法:

NSIndexPath *index ; 
for(int i=0; i<self.slices.contains.count; i++){ 
    index = [NSIndexPath indexPathForRow:i inSection:0]; 
    [self.tableView selectRowAtIndexPath:index animated:NO scrollPosition:UITableViewScrollPositionNone]; 
} 

这将在迭代你的tableview的所有单元格,选择他们(实际上它们添加到

tableview indexPathsForSelectedRows 
相关问题