A small but very powerful API addition in iOS 15 is the new configurationUpdateHandler property on UICollectionViewCell, UITableViewCell, UITableViewHeaderFooterView, and even UIButton. developer.apple.com/documentation/…
TL;DR: you can set a closure which is called anytime the view's state changes.
If you're familiar with the updateConfiguration(using state:) method introduced last year in iOS 14, this is a closure-based version of it — so you don't need a subclass just to override that anymore!
This handler makes it really easy to customize the configuration & appearance of your cells/headers/buttons for different states, directly inline.
It's a great place to apply a new configuration, but you can update any other properties of the cell/view in the handler, too!
The best part is that this handler is guaranteed to run before the cell/view first displays. So if you want, you can do _all_ your configuration of the cell/view _inside_ the handler — meaning you write a single code path, which runs for both initial setup and subsequent updates!
Since it's a closure, you can capture references in it too (though be careful about what you retain).
And to run the handler again anytime (e.g. to update the view when a captured reference changes), just call setNeedsUpdateConfiguration() to invalidate the view's configuration!
For a refresher on updateConfiguration(using state:), setNeedsUpdateConfiguration(), and other configuration-related APIs, check out the Modern Cell Configuration video from WWDC20 last year: developer.apple.com/videos/play/ww…
And to learn about all the new UIButton configuration APIs introduced this year in iOS 15, including examples of how you can use the configurationUpdateHandler with buttons, watch the "Meet the UIKit button system" video: developer.apple.com/wwdc21/10064
• • •
Missing some Tweet in this thread? You can try to
force a refresh
iOS 15 introduces a new way for you to conveniently update content displayed in existing cells in UICollectionView and UITableView: reconfigure.
When and why should you use reconfigure? How is it different from reloading an item or row?
Let’s dive in with a quick thread.
You can think of reconfigure as a lighter-weight version of reload.
Reload: replaces the existing cell with a new cell.
Reconfigure: allows you to directly update the existing cell.
Because reconfigure doesn’t request/create a new cell, it’s significantly more efficient!
How does reconfigure work?
For each item/row you reconfigure:
- If there’s no existing cell, it’s a no-op!
- Otherwise, the collection/table view calls your cell provider again, but with special behavior to return the *existing* cell when you dequeue one for that index path.
For starters, there’s well over a decade of implementation behind these properties and the different cell styles, with layers upon layers of complexity to preserve binary compatibility with all the apps using them. (Old apps in the App Store need to keep working without updates!)
Unfortunately, because these built-in cell textLabel/imageView properties expose the entire API surface of UILabel/UIImageView, that means apps are doing all sorts of unexpected things to these views that weren’t intended to be supported (but aren’t explicitly disallowed, either)