{"id":275,"date":"2020-05-02T16:26:01","date_gmt":"2020-05-02T14:26:01","guid":{"rendered":"https:\/\/mic.st\/blog\/?p=275"},"modified":"2020-05-02T16:26:03","modified_gmt":"2020-05-02T14:26:03","slug":"how-to-easily-animate-updates-of-a-tableview-in-swift","status":"publish","type":"post","link":"https:\/\/mic.st\/blog\/how-to-easily-animate-updates-of-a-tableview-in-swift\/","title":{"rendered":"How to easily animate updates of a tableview in Swift"},"content":{"rendered":"\n<p>Currently I work on a side project that uses a lot of <code>UITableViews<\/code> for all the different settings within the app. Whenever you change the the datasource of your tableview you need to update it. Here I want to show you a simple way (probably the easiest) how to easily animate updates of your tableview in Swift.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Updating <code>UITableView<\/code> without animations<\/h2>\n\n\n\n<p>If you do not want to have any animations when updating your <code>UITableView<\/code> you can just call <code>reloadData()<\/code> as you probably know. You can read more about it here: <a href=\"https:\/\/developer.apple.com\/documentation\/uikit\/uitableview\/1614862-reloaddata\" target=\"_blank\" rel=\"noopener\">https:\/\/developer.apple.com\/documentation\/uikit\/uitableview\/1614862-reloaddata<\/a>. This approach does the job but it does not look very nice:<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video controls src=\"https:\/\/mic.st\/blog\/wp-content\/uploads\/2020\/05\/withoutAnimation.mov\"><\/video><figcaption>Updating <code>UITableView<\/code> without animations using <code>reloadData()<\/code>.<\/figcaption><\/figure>\n\n\n\n<p>Updating your tableview without any animations does not look very nice. You should avoid that.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"500\" src=\"https:\/\/mic.st\/blog\/wp-content\/uploads\/2020\/05\/reloadDatameme-1.jpg\" alt=\"Animate tableview updates with reloadSections rather than using reloadData.\" class=\"wp-image-292\" srcset=\"https:\/\/mic.st\/blog\/wp-content\/uploads\/2020\/05\/reloadDatameme-1.jpg 500w, https:\/\/mic.st\/blog\/wp-content\/uploads\/2020\/05\/reloadDatameme-1-300x300.jpg 300w, https:\/\/mic.st\/blog\/wp-content\/uploads\/2020\/05\/reloadDatameme-1-150x150.jpg 150w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/figure>\n\n\n\n<p>Luckily, there is a very simple way to trigger animated updates in a <code>UITableView<\/code>. Here come&#8217;s <code>reloadSections(_:with:)<\/code> (see: <a href=\"https:\/\/developer.apple.com\/documentation\/uikit\/uitableview\/1614954-reloadsections\" target=\"_blank\" rel=\"noopener\">https:\/\/developer.apple.com\/documentation\/uikit\/uitableview\/1614954-reloadsections<\/a>). <\/p>\n\n\n\n<p>As the first parameter you pass in an <code>IndexSet<\/code> which basically is an array of integers for the sections you want to update. With the second parameter (<code>UITableView.RowAnimation<\/code>) you define the animation of the updates. Here you can see the documentation for all the available animations: <a href=\"https:\/\/developer.apple.com\/documentation\/uikit\/uitableview\/rowanimation\" target=\"_blank\" rel=\"noopener\">https:\/\/developer.apple.com\/documentation\/uikit\/uitableview\/rowanimation<\/a>.<\/p>\n\n\n\n<p>If you want to update the whole <code>UITableView<\/code> simply pass in an array of all the sections. For this purpose I just created an instance of <code>IndexSet<\/code> like that:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Swift\" data-shcb-language-slug=\"swift\"><span><code class=\"hljs language-swift\"><span class=\"hljs-type\">IndexSet<\/span>(integersIn: <span class=\"hljs-number\">0<\/span>...tableView.numberOfSections - <span class=\"hljs-number\">1<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Swift<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">swift<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the next two chapters I give you a litte preview how some of the animations can look like.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Animate tableview updates using <code>.automatic<\/code> argument<\/h2>\n\n\n\n<p>There is an <code>.automatic<\/code> animation that looks pretty nice for a lot of use cases. Here you can see how it looks in my <code>UITableView<\/code> from the first example:<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video controls src=\"https:\/\/mic.st\/blog\/wp-content\/uploads\/2020\/05\/automatic.mov\"><\/video><figcaption>Updating <code>UITableView<\/code>&#8216;s datasource with <code>.automatic<\/code> animation.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Animate tableview updates using <code>.left<\/code> and <code>. right<\/code> argument with <code>UISegmentedControl<\/code><\/h2>\n\n\n\n<p>When you animate your tableview updates with <code>.automatic<\/code>, the updates look already way better than without any animations. However I decided to make it look more connected to the <code>UISegmentedControl<\/code> that triggers the updates. I wanted to visually reference to the sliding animation of the <code>UISegmentedControl<\/code> by making the updates slide in and out from left and right. To achieve this, I used the <code>.left<\/code> and <code>.right<\/code> animation parameter.<\/p>\n\n\n\n<p>All I had to do is to pick the right animation based on the selected segment index:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Swift\" data-shcb-language-slug=\"swift\"><span><code class=\"hljs language-swift shcb-code-table shcb-line-numbers\"><span class='shcb-loc'><span><span class=\"hljs-meta\">@objc<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">func<\/span> <span class=\"hljs-title\">changedSegmentedControl<\/span><span class=\"hljs-params\">(<span class=\"hljs-number\">_<\/span> sender: UISegmentedControl)<\/span><\/span> {\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-comment\">\/\/ Here you do your updates<\/span>\n<\/span><\/span><span class='shcb-loc'><span>  tableView.reloadSections(<span class=\"hljs-type\">IndexSet<\/span>(integersIn: <span class=\"hljs-number\">0<\/span>...tableView.numberOfSections - <span class=\"hljs-number\">1<\/span>), with: sender.selectedSegmentIndex == <span class=\"hljs-number\">0<\/span> ? .<span class=\"hljs-keyword\">left<\/span> : .<span class=\"hljs-keyword\">right<\/span>)\n<\/span><\/span><span class='shcb-loc'><span>}\n<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Swift<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">swift<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This is how it looks now:<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video controls src=\"https:\/\/mic.st\/blog\/wp-content\/uploads\/2020\/05\/leftright.mov\"><\/video><\/figure>\n\n\n\n<p>Happy Coding!<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/developer.apple.com\/documentation\/uikit\/uitableview\/1614862-reloaddata\" target=\"_blank\" rel=\"noopener\">https:\/\/developer.apple.com\/documentation\/uikit\/uitableview\/1614862-reloaddata<\/a><\/li><li><a href=\"https:\/\/developer.apple.com\/documentation\/uikit\/uitableview\/1614862-reloaddata\" target=\"_blank\" rel=\"noopener\">https:\/\/developer.apple.com\/documentation\/uikit\/uitableview\/1614862-reloaddata<\/a><\/li><li><a href=\"https:\/\/developer.apple.com\/documentation\/uikit\/uitableview\/rowanimation\" target=\"_blank\" rel=\"noopener\">https:\/\/developer.apple.com\/documentation\/uikit\/uitableview\/rowanimation<\/a><\/li><\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Currently I work on a side project that uses a lot of UITableViews for all the different settings within the app. Whenever you change the the datasource of your tableview you need to update it. Here I want to show you a simple way (probably the easiest) how to easily animate updates of your tableview&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[3],"tags":[37,35,36],"class_list":["post-275","post","type-post","status-publish","format-standard","hentry","category-ios-development","tag-animations","tag-swift","tag-tableview"],"_links":{"self":[{"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/posts\/275","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/comments?post=275"}],"version-history":[{"count":0,"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/posts\/275\/revisions"}],"wp:attachment":[{"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/media?parent=275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/categories?post=275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/tags?post=275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}