2013-07-30 63 views
0

我需要添加一个字符串,然后选择添加的行,以保留之前做出的所有选择。只有在设置了断点的情况下,UITableView单元格选择才能正常工作

由于架构问题,我要叫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

它可以通过调用在注释功能先更新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]; 
    }]; 
} 
相关问题