Reload sections in UITableView

后端 未结 6 2393
执笔经年
执笔经年 2021-02-13 11:34

I am writing an app that notifies user when its time to take his/her medicine. The label at the top of the page shows a date and the tableView gets populated with m

相关标签:
6条回答
  • 2021-02-13 12:03

    Here is the method, you can pass section details in different ways

    [self.tableView reloadSections:[[NSIndexSet alloc] initWithIndex:1] withRowAnimation:NO];
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
    

    Reloading particular sections improves performance for the table view as well some time it also avoid some issues like floating/moving custom headers-footers in your view. SO try to use reloadSection than relaodData whenever possible

    0 讨论(0)
  • 2021-02-13 12:04

    There are few functions supported in UITableView for reloading data. See section Reloading the Table View of doc.

    • reloadData
    • reloadRowsAtIndexPaths:withRowAnimation:
    • reloadSections:withRowAnimation:
    • reloadSectionIndexTitles

    Also, see this similar thread, a part from it:

    NSRange range = NSMakeRange(0, 1);
    NSIndexSet *section = [NSIndexSet indexSetWithIndexesInRange:range];                                     
    [self.tableView reloadSections:section withRowAnimation:UITableViewRowAnimationNone];
    

    Also, it is not necessary to reload specific sections, you can just adjust your dataset and call [tableView reloadData], it will load the visible cells only. A part from doc:

    Call this method to reload all the data that is used to construct the table, including cells, section headers and footers, index arrays, and so on. For efficiency, the table view redisplays only those rows that are visible. It adjusts offsets if the table shrinks as a result of the reload.

    0 讨论(0)
  • 2021-02-13 12:08

    Use this method to reload your section

    - (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
    
    0 讨论(0)
  • 2021-02-13 12:13

    Swift 3

    NSIndexSet is nothing special

    tableView.reloadSections([1, 2, 3], with: .none)
    

    It's just an array. Crazy.

    0 讨论(0)
  • 2021-02-13 12:17

    You can use this also--

    indexPath.section - `UITableView section which you want to reload .

     NSIndexSet *section = [NSIndexSet indexSetWithIndex:indexPath.section];
    [self.tableView reloadSections:section withRowAnimation:UITableViewRowAnimationNone];
    
    0 讨论(0)
  • 2021-02-13 12:28

    Update for quick usage in Swift (as of 2.2)

    // index 0 being the section number 1 that you will reload
    tableView.reloadSections(NSIndexSet(index: 0), withRowAnimation: .None)
    
    0 讨论(0)
提交回复
热议问题