How should I use UIConfigurationStateCustomKey?

Hi!

Beginning

I would like to know about UIConfigurationStateCustomKey.

Documentation

In a documentation we can find example where CellConfiguration has been extended and later CustomCell subclass get a property to update cell if needed.

A part of documentation:

Code Block
// Declare a custom key for a custom isArchived state.
extension UIConfigurationStateCustomKey {
static let isArchived = UIConfigurationStateCustomKey("com.my-app.MyCell.isArchived")
}
// Declare an extension on the cell state structure to provide a typed property for this custom state.
extension UICellConfigurationState {
var isArchived: Bool {
get { return self[.isArchived] as? Bool ?? false }
set { self[.isArchived] = newValue }
}
}
class MyCell: UICollectionViewCell {
// This is an existing custom property of the cell.
var isArchived: Bool {
didSet {
// Ensure that an update is performed whenever this property changes.
if oldValue != isArchived {
setNeedsUpdateConfiguration()
}
}
}
}


Documentation example

Look at it carefully. We define custom key and, later, add stored property to ConfigurationState.
But!
We also define a property for MyCell, which has didSet hook with setNeedsUpdateConfiguration invocation.

How do I understand this pattern?

We define a custom property that could be stored in cell configuration.
Later, that property can be changed by a user. So, whenever something happened ( user press a button? ), this cell object receives obj.isArchived = valueFromUser.

But!

If we use this approach, we also define UIContentConfiguration with our UIContentView. That means, that we have our CustomUIContentView: UIContentView with all buttons that we need.
These buttons change state of an associated model object.
So, we define Row with all booleans which are related/mapped to buttons on CustomUIContentView.

Conclusion

There is no point where we should use properties for Cells.

Question

Am I missing something with new API?

Accepted Reply

Please take a look at this answer which explains the motivations behind UIConfigurationState and some reasons why you might want to add your own custom states. If you have specific questions that aren't answered by that, please feel free to ask them.

Replies

Please take a look at this answer which explains the motivations behind UIConfigurationState and some reasons why you might want to add your own custom states. If you have specific questions that aren't answered by that, please feel free to ask them.