我正在开发iOS 5项目,并且我正在使用表格视图来选择将添加到集合中的项目。
我正确地将AllowsMultipleSelectionDuringEditing
设置为YES以使子弹位于左侧并且contentView
缩进。
现在,我的问题是我有一个预先选择的项目列表,用户在进入编辑模式时应该看到这些项目。我查看了文档,但是我没有找到任何关于它的信息,以便在进入编辑模式时预先选择索引路径列表并启用多个选择。 Apple是否有办法预先选择一定数量的项目,还是应该自己开发此功能?
答案 0 :(得分:6)
好的,我发现了如何做到这一点以及一种优雅而优雅的方式,它比例外更简单:
UITableView具有选择行的方法selectRowAtIndexPath:animated:scrollPosition:
。
完成我要找的内容的最佳方法是创建NSSet
NSIndexPaths
,并在编辑模式下设置UITableView
后立即迭代该集并选择单元格 - 酮
例如以这种方式:
- (NSArray *)_preselectedIndexPaths
{
NSMutableSet *preselectedItems = [NSMutableSet set];
NSUInteger s = 0, r = 0;
for (NSArray *section in self.data) {
for (id item in section) {
if ([item shouldBePreselected]) { //this is the condition
[preselectedItems addObject:[NSIndexPath indexPathForRow:r inSection:s]];
}
r++;
}
s++;
}
return [preselectedItems allObjects];
}
然后,选择表视图中的项目:
[self.tableView setEditing:![self.tableView isEditing] animated:YES];
for (NSIndexPath *ip in [self _preselectedIndexPaths])
{
[self.tableView selectRowAtIndexPath:ip
animated:YES
scrollPosition:UITableViewScrollPositionNone];
}
我希望这可以帮助别人。