2013-07-30 67 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]; 
    }]; 
} 
相關問題