Home > other >  What is "stable identifier" for SwiftUI Cell?
What is "stable identifier" for SwiftUI Cell?

Time:02-04

While listening Use SwiftUI with UIKit (16:54), I heard, that the reporter said:

"When defining swipe actions, make sure your buttons perform their actions using a stable identifier for the item represented. Do not use the index path, as it may change while the cell is visible, causing the swipe actions to act on the wrong item."
- what??? All these years I was fighting with prepareForReuse() and indexPath in cells trying to somehow fix bugs related to cell reusing.


What is this "stable identifier"?
Why does no one talk about it?
On stackoverflow you can find answers only related to prepareForReuse() function. No "stable identifier".
Is it reuseIdentifier?
If so, how I suppose to use it?
Creating for each cell its own reuseIdentifier, like this:

for index in 0..<dataSourceArray.count {
    tableView.register(MyTableViewCell.self, forCellReuseIdentifier: "ReuseIdForMyCell"   "\(index)")
}
    

CodePudding user response:

She made a mistake, if you download the sample associated with the video you'll see the deleteHandler captures the item from outside already, it doesn't look it up again when the handler is invoked. She was trying to say that if you look up an item in the handler then there is a chance if other rows have been added or removed then its index will have changed so if you use the rows old index to look up the item then you would delete the wrong one. But since no lookup is required that will never happen, so she shouldn't have even mentioned it. Here is the code in question:

    // Configures a list cell to display a medical condition.
    private func configureMedicalConditionCell(_ cell: UICollectionViewListCell, for item: MedicalCondition) {
        cell.accessories = [.delete()]
        // Create a handler to execute when the cell's delete swipe action button is triggered.
        let deleteHandler: () -> Void = { [weak self] in
            // Make sure to use the item itself (or its stable identifier) in any escaping closures, such as
            // this delete handler. Do not capture the index path of the cell, as a cell's index path will
            // change when other items before it are inserted or deleted, leaving the closure with a stale
            // index path that will cause the wrong item to be deleted!
            self?.dataStore.delete(item)
        }
        
        // Configure the cell with a UIHostingConfiguration inside the cell's configurationUpdateHandler so
        // that the SwiftUI content in the cell can change based on whether the cell is editing. This handler
        // is executed before the cell first becomes visible, and anytime the cell's state changes.
        cell.configurationUpdateHandler = { cell, state in
            cell.contentConfiguration = UIHostingConfiguration {
                MedicalConditionView(condition: item, isEditable: state.isEditing)
                    .swipeActions(edge: .trailing) {
                        Button(role: .destructive, action: deleteHandler) {
                            Label("Delete", systemImage: "trash")
                        }
                    }
            }
        }
    }
  • Related