How can I maintain display order in UITableView using Core Data?

后端 未结 4 1621
说谎
说谎 2020-12-12 18:01

I\'m having some trouble getting my Core Data entities to play nice and order when using an UITableView.

I\'ve been through a number of tutorials and other questions

4条回答
  •  遥遥无期
    2020-12-12 18:20

    I took a look at your code and this might work better:

    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {         
    
        NSUInteger fromIndex = fromIndexPath.row;  
        NSUInteger toIndex = toIndexPath.row;
    
        if (fromIndex == toIndex) {
            return;
        }
    
        FFObject *affectedObject = [self.fetchedResultsController.fetchedObjects objectAtIndex:fromIndex];  
        affectedObject.displayOrderValue = toIndex;
    
        NSUInteger start, end;
        int delta;
    
        if (fromIndex < toIndex) {
            // move was down, need to shift up
            delta = -1;
            start = fromIndex + 1;
            end = toIndex;
        } else { // fromIndex > toIndex
            // move was up, need to shift down
            delta = 1;
            start = toIndex;
            end = fromIndex - 1;
        }
    
        for (NSUInteger i = start; i <= end; i++) {
            FFObject *otherObject = [self.fetchedResultsController.fetchedObjects objectAtIndex:i];  
            NSLog(@"Updated %@ / %@ from %i to %i", otherObject.name, otherObject.state, otherObject.displayOrderValue, otherObject.displayOrderValue + delta);  
            otherObject.displayOrderValue += delta;
        }
    
        [self FF_fetchResults];  
    }
    

提交回复
热议问题