我需要添加一个字符串,然后选择添加的行,保留之前所做的所有选择。
由于架构问题,我必须调用refreshSelections
,它首先取消选择,然后选择两次。
事实上,在添加一行之后,所有选择都会闪烁,并且只选择添加的行。如果我添加另一行,则所有旧选择将再次闪烁,并选择新行。
但是如果我在第一个refreshSelections
之后的任何地方设置了一个断点,它就可以正常工作。
我认为这是一些并发问题,但我不知道如何解决它。
// This is called
-(void)addString:(NSString *)text
{
stringsList = [stringsList arrayByAddingObject:text];
[self applyFilter:self.searchBar.text];
// Any breakpoint after will help
[filterSelect addStringToSelected:text];
}
- (void)applyFilter:(NSString *)filterString
{
[filterSelect reloadData:YES];
}
- (void)refreshSelections
{
for (NSUInteger row = 0; row < stringsList.count; ++row) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:row inSection:0];
[stringsTableView deselectRowAtIndexPath:indexPath animated:NO];
}
NSIndexSet *selectedIndexes = [stringsList indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
return [selectedStrings containsObject:obj];
}];
[selectedIndexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
[stringsTableView selectRowAtIndexPath:[NSIndexPath indexPathForItem:idx inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
}];
}
- (void)reloadData:(BOOL)saveOrder
{
stringsList = [self.dataSource stringsToDisplayInMultipleStringsSelect:self];
[stringsTableView reloadData];
[self refreshSelections];
}
- (void)addStringToSelected:(NSString *)string
{
[self storeSelectedString:string];
[self refreshSelections:1];
}
我所有的WAT。
答案 0 :(得分:0)
可以通过在注释下调用函数之前添加0.1s延迟来解决,以便首先更新UI。
- (void)addCustomStringControllerDelegate:(AddCustomStringController *)sender didFinishWithText:(NSString *)text
{
if (text.length) {
stringsList = [stringsList arrayByAddingObject:text];
[self applyFilter:self.searchBar.text];
double delayInSeconds = 0.1;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[filterSelect addStringToSelected:text];
});
[selectedValuesTableView reloadData];
}
}
但我认为,取消选择所有行然后选择我需要的行是不正确的。所以我选择了(de)只选择我应该的行的方式。
- (void)refreshSelections
{
NSArray *selectedBefore = stringsTableView.indexPathsForSelectedRows;
NSIndexSet *toSelect = [stringsList indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
return [selectedStrings containsObject:obj];
}];
NSMutableArray *toDeselect = [NSMutableArray new];
for (NSIndexPath *selected in selectedBefore) {
if (![toSelect containsIndex:selected.row]) {
[toDeselect addObject:selected];
}
}
for (NSIndexPath *deselect in toDeselect) {
[stringsTableView deselectRowAtIndexPath:deselect animated:NO];
}
[toSelect enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
[stringsTableView selectRowAtIndexPath:[NSIndexPath indexPathForItem:idx inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
}];
}