Have a reloadData for a UITableView animate when changing

前端 未结 17 2937
感动是毒
感动是毒 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:10

    Native UITableView animations in Swift

    Insert and delete rows all at once with tableView.performBatchUpdates so they occur simultaneously. Building on the answer from @iKenndac use methods such as:

    • tableView.insertSections
    • tableView.insertRows
    • tableView.deleteSections
    • tableView.deleteRows

    Ex:

     tableView.performBatchUpdates({
       tableView.insertSections([0], with: .top)
     })
    

    This inserts a section at the zero position with an animation that loads from the top. This will re-run the cellForRowAt method and check for a new cell at that position. You could reload the entire table view this way with specific animations.

    Re: the OP question, a conditional flag would have been needed to show the cells for the alternate table view state.

提交回复
热议问题