Have a reloadData for a UITableView animate when changing

前端 未结 17 2892
感动是毒
感动是毒 2020-11-28 17:25

I have a UITableView that has two modes. When we switch between the modes I have a different number of sections and cells per section. Ideally, it would do some cool anima

17条回答
  •  执念已碎
    2020-11-28 17:58

    In my case, I wanted to add 10 more rows into the tableview (for a "show more results" type of functionality) and I did the following:

      NSInteger tempNumber = self.numberOfRows;
      self.numberOfRows += 10;
      NSMutableArray *arrayOfIndexPaths = [[NSMutableArray alloc] init];
      for (NSInteger i = tempNumber; i < self.numberOfRows; i++) {
        [arrayOfIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
      }
      [self.tableView beginUpdates];
      [self.tableView insertRowsAtIndexPaths:arrayOfIndexPaths withRowAnimation:UITableViewRowAnimationTop];
      [self.tableView endUpdates];
    

    In most cases, instead of "self.numberOfRows", you would usually use the count of the array of objects for the tableview. So to make sure this solution works well for you, "arrayOfIndexPaths" needs to be an accurate array of the index paths of the rows being inserted. If the row exists for any of this index paths, the code might crash, so you should use the method "reloadRowsAtIndexPaths:withRowAnimation:" for those index pathds to avoid crashing

提交回复
热议问题