Reload section without reloading section header

后端 未结 7 600
离开以前
离开以前 2020-12-08 08:03

I have a UITableView, where there is a UISegmentedControl in the header view. It should work exactly like in the App Store app: As the user scrolls

7条回答
  •  广开言路
    2020-12-08 08:19

    Maybe you should try with

    [self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationLeft] //or UITableViewRowAnimationRight
    

    However, I'm not sure but I think it can rise some error in the case where you have less rows to reload than previously.


    Edit

    I think you could deal with [tableView beginUpdates] and [tableView endUpdates] to solve your problem.

    For example, you have 2 arrays of data to display. Let name them oldArray and newArray. A sample of how what you could do :

    - (void)selectedSegmentIndexChanged:(UISegmentedControl *)sender
    {
        [self.tableView setDataSource: newArray];
        int nbRowToDelete = [oldArray count];
        int nbRowToInsert = [newArray count];
    
        NSMutableArray *indexPathsToInsert = [[NSMutableArray alloc] init];
        for (NSInteger i = 0; i < nbRowToInsert; i++) {
            [indexPathsToInsert addObject:[NSIndexPath indexPathForRow:i inSection:section]];
        }
    
        NSMutableArray *indexPathsToDelete = [[NSMutableArray alloc] init];
        for (NSInteger i = 0; i < nbRowToDelete; i++) {
            [indexPathsToDelete addObject:[NSIndexPath indexPathForRow:i inSection:section]];
        }
    
        [self.tableView beginUpdates];
        [self.tableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:UITableViewRowAnimationLeft];
        [self.tableView insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:UITableViewRowAnimationRight];
        [self.tableView endUpdates];
    }
    

提交回复
热议问题