Checkboxes in UITableView State Persistence
Think of a table view cell as the patient info form you get in the waiting room at a doctor's office. The catch is that the doctor's office reuses the forms, and doesn't erase the old info from them.
In your cellForRowAt method, it's up to you to completely restore the cell to its default state before using it again. You have to reset everything to it's default state.
As an alternative to doing that resetting in cellForRowAt, you can use a custom cell and implement the prepareForReuse()
method.
The key thing is that you need to force every view in the cell to it's default state before you begin configuring it.
checkbox persists after a row is deleted from a table
The persistant checkbox icon is because of the reusableCells. If you are using reusableCells then you need to reset them to "stock" as it were every time you use it for a different cell.
As for the crashing, you are trying to delete indexPaths by using integers. And indexPath is a path (section and row). Where you are adding the int, add a newly created index path instead (I'm assuming there is only one section, so it would be section 0 row i). If you need code examples for all of this let me know.
CheckBox on tableview duplicating, Swift, iOS
You can use tableView.dequeueReusableCell(_)
, The problem is, you didn't maintain the status of the selected cells.
Example :
class viewController: UIVieWController, UITableViewDelegate, UITableViewDataSource {
var selectedCellList = [IndexPath]()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "FeaturesCell") as! FeaturesCell
cell.featuresLabel.text = self.items[indexPath.row]
if let _ = selectedCellList.index(of: indexPath) {
// Cell selected, update check box image with tick mark
cell.checkImage.image = #imageLiteral(resourceName: "tick-inside-circle")
} else {
// Cell note selected, update check box image without tick mark
cell.checkImage.image = #imageLiteral(resourceName: "No-tick-inside-circle")
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
pickedFeatures.append(items[indexPath.row])
if let index = selectedCellList.index(of: indexPath) {
selectedCellList.remove(at: index)
} else {
selectedCellList.append(indexPath)
}
tableView .reloadRows(at: [indexPath], with: .automatic)
}
}
UISearchBar with table view and checkbox tutorial
There are many ways to do this. You can't expect tutorials to do exactly what you are doing.
There are many ways of doing this. One of the ways is to store the state of the checked cells in an NSArray
and then load the state in cellForRowAtIndexPath
data source method.
Data persistence with reusable cells
viewModels hold the information needed to setup each cell right? And you receive the viewModels from the service?
If so, when the user changes a specific checkbox, you should update the according viewModel. Thus, when you call setupCell
inside cellForRowAt
you should pass the updated info of each viewModel, resulting in the correct state of each checkbox.
Related Topics
Nsurl Fail Able Initialiser Initwithstring: Does Not Return Nil on Empty String in Swift
Swift Why Isn't My Date Object That's (Equatable) Equal After Converting It to a String and Back
Change Mkmarkerannotationview Size
Swift: Nil Error When Using Self.Moc.Save() to Save in Core Data
Getch() Equivalent in Swift: Read a Single Character from Stdin Without a Newline
Using Tvposterimage in Tvuikit for Tvos 12
Swiftui: Unexpected Animation When Using a Non @State Var
Realitykit - How to Add Motion to a Loaded Modelentity from Usdz File
How to Create Lazy Combinations
Xcode Gm: No Swift Language for Os X Command Line Tool Project
Stanford Calculator App Keeps Crashing
Bulk Fix Hundreds of "#Selector Not Exposed to Objective-C" Errors in Xcode 9 or 9.1
How to Move a Model and Generate Its Collision Shape at the Same Time
Accessing and Manipulating Array Item in an Environmentobject