好吧,我发现了如何做到这一点以及良好和优雅的方式,它是简单除了例外:
的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];
}
我希望这可以帮助别人。