0

我需要添加一个字符串,然后选择添加的行,保留之前所做的所有选择。

由于架构问题,我必须调用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。

4

1 回答 1

0

可以通过在调用注释下的函数先更新UI之前添加0.1s的延迟来解决。

- (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];
    }
}

但我认为,我取消选择所有行然后选择我需要的行是不正确的。因此,我选择了仅(取消)选择我应该选择的行的方式。

- (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];
    }];
}
于 2013-07-31T12:24:45.417 回答