0

文字列を追加してから、以前に行ったすべての選択を維持したまま、追加された行を選択する必要があります。

アーキテクチャの問題により、 を呼び出す必要がありますrefreshSelections。これは、最初に選択を解除し、その後に 2 回選択します。

実際、行を追加した後、すべての選択が点滅し、追加された行のみが選択されます。別の行を追加すると、すべての古い選択が再び点滅し、新しい行が選択されたままになります。

しかし、最初の後に任意の場所にブレークポイントを設定すると、問題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.1 秒の遅延を追加することで解決できます。

- (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 に答える