2010 से यह प्रश्न मुझे मिला जब मैं गुगल रहा था। आईओएस 5.0 के बाद से, अब हमारे पास -[UITableView moveRowAtIndexPath:toIndexPath]
भी है जिसे आप वास्तव में भी संभालना चाहते हैं। यहां एक फ़ंक्शन है जो दो सरणी की तुलना करता है और हटाए जाने, सम्मिलित करने और संचालन को स्थानांतरित करने के लिए उपयुक्त इंडेक्सपाथ उत्पन्न करता है।
- (void) calculateTableViewChangesBetweenOldArray:(NSArray *)oldObjects
newArray:(NSArray *)newObjects
sectionIndex:(NSInteger)section
indexPathsToDelete:(NSArray **)indexPathsToDelete
indexPathsToInsert:(NSArray **)indexPathsToInsert
indexPathsToMove:(NSArray **)indexPathsToMove
destinationIndexPaths:(NSArray **)destinationIndexPaths
{
NSMutableArray *pathsToDelete = [NSMutableArray new];
NSMutableArray *pathsToInsert = [NSMutableArray new];
NSMutableArray *pathsToMove = [NSMutableArray new];
NSMutableArray *destinationPaths = [NSMutableArray new];
// Deletes and moves
for (NSInteger oldIndex = 0; oldIndex < oldObjects.count; oldIndex++) {
NSObject *object = oldObjects[oldIndex];
NSInteger newIndex = [newObjects indexOfObject:object];
if (newIndex == NSNotFound) {
[pathsToDelete addObject:[NSIndexPath indexPathForRow:oldIndex inSection:section]];
} else if (newIndex != oldIndex) {
[pathsToMove addObject:[NSIndexPath indexPathForRow:oldIndex inSection:section]];
[destinationPaths addObject:[NSIndexPath indexPathForRow:newIndex inSection:section]];
}
}
// Inserts
for (NSInteger newIndex = 0; newIndex < newObjects.count; newIndex++) {
NSObject *object = newObjects[newIndex];
if (![oldObjects containsObject:object]) {
[pathsToInsert addObject:[NSIndexPath indexPathForRow:newIndex inSection:section]];
}
}
if (indexPathsToDelete) *indexPathsToDelete = [pathsToDelete copy];
if (indexPathsToInsert) *indexPathsToInsert = [pathsToInsert copy];
if (indexPathsToMove) *indexPathsToMove = [pathsToMove copy];
if (destinationIndexPaths) *destinationIndexPaths = [destinationPaths copy];
}
इसका उपयोग करने के तरीके पर एक उदाहरण। मान लें कि आप लोगों की एक तालिका प्रदर्शित कर रहे हैं, जिसे आप सरणी self.people
में रखते हैं। सेक्शन इंडेक्स जहां लोग प्रदर्शित होते हैं 0.
- (void) setPeople:(NSArray <Person *> *)newPeople {
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView beginUpdates];
NSArray *rowsToDelete, *rowsToInsert, *rowsToMove, *destinationRows;
[self calculateTableViewChangesBetweenOldArray:self.people
newArray:newPeople
sectionIndex:0
indexPathsToDelete:&rowsToDelete
indexPathsToInsert:&rowsToInsert
indexPathsToMove:&rowsToMove
destinationIndexPaths:&destinationRows
];
self.people = newPeople;
[self.tableView deleteRowsAtIndexPaths:rowsToDelete withRowAnimation:UITableViewRowAnimationFade];
[self.tableView insertRowsAtIndexPaths:rowsToInsert withRowAnimation:UITableViewRowAnimationFade];
[rowsToMove enumerateObjectsUsingBlock:^(NSIndexPath * _Nonnull oldIndexPath, NSUInteger idx, BOOL * _Nonnull stop) {
NSIndexPath *newIndexPath = destinationRows[idx];
[self.tableView moveRowAtIndexPath:oldIndexPath toIndexPath:newIndexPath];
}];
[self.tableView endUpdates];
});
}
ठीक है! यह पाने के लिए खूनी मुश्किल :) –