Have a reloadData for a UITableView animate when changing

前端 未结 17 2953
感动是毒
感动是毒 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 18:11

    The way to approach this is to tell the tableView to remove and add rows and sections with the

    insertRowsAtIndexPaths:withRowAnimation:,
    deleteRowsAtIndexPaths:withRowAnimation:,
    insertSections:withRowAnimation: and
    deleteSections:withRowAnimation:

    methods of UITableView.

    When you call these methods, the table will animate in/out the items you requested, then call reloadData on itself so you can update the state after this animation. This part is important - if you animate away everything but don't change the data returned by the table's dataSource, the rows will appear again after the animation completes.

    So, your application flow would be:

    [self setTableIsInSecondState:YES];

    [myTable deleteSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:YES]];

    As long as your table's dataSource methods return the correct new set of sections and rows by checking [self tableIsInSecondState] (or whatever), this will achieve the effect you're looking for.

提交回复
热议问题