reloadRowsAtIndexPaths:withRowAnimation: crashes my app

后端 未结 8 1992
谎友^
谎友^ 2021-02-07 01:33

I got a strange problem with my UITableView: I use reloadRowsAtIndexPaths:withRowAnimation: to reload some specific rows, but the app crashes with an seemingly unre

相关标签:
8条回答
  • 2021-02-07 01:48

    I think the following code might work:

    [self.tableView beginUpdates];
    
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
    
    [self.tableView endUpdates];
    
    0 讨论(0)
  • 2021-02-07 01:48

    After many try, I found "reloadRowsAtIndexPaths" can be only used in certain places if only change the cell content not insert or delete cells. Not any place can use it, even you wrap it in

    [self beginUpdates];
    //reloadRowsAtIndexPaths
    [self endUpdates];
    

    The places I found that can use it are:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath - (IBAction) unwindToMealList: (UIStoryboardSegue *) sender

    Any try from other places like call it from "viewDidLoad" or "viewDidAppear", either will not take effect (For the cell already loaded I mean, reload will not take effect) or cause exception.

    So try to use "reloadRowsAtIndexPaths" only in those places.

    0 讨论(0)
  • 2021-02-07 01:51

    THIS IS OLD. DO NOT USE. I just bumped into this issue when I was calling reloadRowsAtIndexPaths... in order to change the cell to an editing cell containing a UITextField. The error told me I was deleting all of the rows in the table. To solve the problem, I removed:

    [self.tableView beginUpdates];
    NSArray *reloadIndexPath = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:count inSection:section]];
    [self.tableView reloadRowsAtIndexPaths:reloadIndexPath withRowAnimation:UITableViewRowAnimationFade];  
    [self.tableView endUpdates];
    

    and replaced it with

    [self.tableView reloadData];
    
    0 讨论(0)
  • 2021-02-07 01:53

    The problem is that you, probably, have changed size of your table. For example, you have add or remove some source data for table view.

    In that case, when you call reloadData your table is completely reloaded including sizes of sections and number of that sections.

    But when you call reloadRowsAtIndexPaths:withRowAnimation: that parameters of your table view are not reloaded. That causes next problem: when you are trying to reload some cell table check the size of table view and sees that it has been changed. That results in a crash. This method can be used only when you wan't to reload view of the cell (for example, label has changed or you want to change its size).

    Now if you want to remove/add cells from/to table view you should use next approach:

    1. Inform table that its size will be changed by calling method beginUpdates of UITableView
    2. Inform about inserting new row(s) using method - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
    3. Inform about removing row(s) using method - (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
    4. Inform table that its size has been changed by calling method endUpdates of UITableView

    Hope it will be helpful

    0 讨论(0)
  • 2021-02-07 02:00

    I had this problem which was being caused by a block calling reloadRowsAtIndexPaths:withRowAnimation: and a parallel thread calling reloadData. The crash was due to reloadRowsAtIndexPaths:withRowAnimation finding an empty table even though I'd sanity checked numberOfRowsInSection & numberOfSections.

    I took the attitude that I don't really care if it causes an exception. A visual corruption I could live with as a user of the App than have the whole app crash out.

    Here's my solution to this which I'm happy to share and would welcome constructive criticism. If there's a better solution I'm keen to hear it?

    - (void) safeCellUpdate: (NSUInteger) section withRow : (NSUInteger) row {
        // It's important to invoke reloadRowsAtIndexPaths implementation on main thread, as it wont work on non-UI thread
        dispatch_async(dispatch_get_main_queue(), ^{
            NSUInteger lastSection = [self.tableView numberOfSections];
            if (lastSection == 0) {
                return;
            }
            lastSection -= 1;
            if (section > lastSection) {
                return;
            }
            NSUInteger lastRowNumber = [self.tableView numberOfRowsInSection:section];
            if (lastRowNumber == 0) {
                return;
            }
            lastRowNumber -= 1;
            if (row > lastRowNumber) {
                return;
            }
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
            @try {
                if ([[self.tableView indexPathsForVisibleRows] indexOfObject:indexPath] == NSNotFound) {
                    // Cells not visible can be ignored
                    return;
                }
                [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            }
    
            @catch ( NSException *e ) {
                // Don't really care if it doesn't work.
                // It's just to refresh the view and if an exception occurs it's most likely that that is what's happening in parallel.
                // Nothing needs done
                return;
            }
        });
    }
    
    0 讨论(0)
  • 2021-02-07 02:00

    The app crashes because you have made some changes to your tableView. Either you have added or deleted some rows to the tableView. Hence when the view controller asks your model controller class for data, there is a mismatch in the indexPaths. Since the indexPaths have changed after modification.

    So either you simply remove the call

    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
    

    or replace it with

    [self.tableView reloadData];
    

    Calling reloadData checks your number of sections, number of rows in each section and then reloads the whole thing.

    0 讨论(0)
提交回复
热议问题