No matter what I do I cannot keep the checkmark item selected after scrolling. I know this has to do with the fact that the cells are reused when items are visible but I'm not sure how to handle this.
The following code successfully shows the right item selected on the first load, the issue is that once I start scrolling it randomly keeps selecting other items.
What is the proper way to keep the selected item after scrolling and prevent from selecting unwanted items/rows?
class CategoriesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var categories: Results<Category>! // I'm getting this from the Realm data base
var categoryTracker:Category? // I'm getting this from other view controler
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return categories.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "categoriesCell", for: indexPath) as! CategoriesCell
let categoryList = categories[indexPath.row]
cell.labelCategoryName.text = categoryList.categoryName
if categories[indexPath.row].categoryID == self.categoryTracker!.categoryID{
cell.accessoryType = .checkmark
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// show checkmark on category for selected item and uncheck the rest
for row in 0..<tableView.numberOfRows(inSection: indexPath.section) {
if let cell = tableView.cellForRow(at: IndexPath(row: row, section: indexPath.section)) {
cell.accessoryType = row == indexPath.row ? .checkmark : .none
}
}
}
}
Images:
You are right. This happens because when a cell with .checkmark is reused, it is not reseting its accessory type back to .none
You should update your:
if categories[indexPath.row].categoryID == self.categoryTracker!.categoryID{
cell.accessoryType = .checkmark
}
to:
if categories[indexPath.row].categoryID == self.categoryTracker!.categoryID {
cell.accessoryType = .checkmark
} else {
cell.accessoryType = .none
}
Related
I am making a music genre picking application and when I go to my table to select genres, I select a row and it selects a random row about 10 or so down from my selection.
My code for the selection is:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let genresFromLibrary = genrequery.collections
let rowitem = genresFromLibrary![indexPath.row].representativeItem
print(rowitem?.value(forProperty: MPMediaItemPropertyGenre) as! String
)
if let cell = tableView.cellForRow(at: indexPath)
{
cell.accessoryType = .checkmark
}
}
override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath)
{
cell.accessoryType = .none
}
}
Cells are reused by default when cellForRowAtIndexPath is called. This causes the cells to have the wrong data when you don't keep track of the indexPaths that have been selected. You need to keep track of the index paths that are currently selected so you can show the appropriate accessory type in your table view.
One way of doing it is to have a property in your UITableViewController that just stores the index paths of the selected cells. It can be an array or a set.
var selectedIndexPaths = Set<IndexPath>()
When you select a row on didSelectRowAt, add or remove the cell from selectedIndexPaths, depending on whether the index path is already in the array or not:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if selectedIndexPaths.contains(indexPath) {
// The index path is already in the array, so remove it.
selectedIndexPaths.remove(indexPathIndex)
} else {
// The index path is not part of the array
selectedIndexPaths.append(indexPath)
}
// Show the changes in the selected cell (otherwise you wouldn't see the checkmark or lack thereof until cellForRowAt got called again for this cell).
tableView.reloadRows(at: [indexPath], with: .none)
}
Once you have this, on your cellForRowAtIndexPath, check if the indexPath is in the selectedIndexPaths array to choose the accessoryType.
if selectedIndexPaths.contains(indexPath) {
// Cell is selected
cell.accessoryType = .checkmark
} else {
cell.accessoryType = .none
}
This should solve the problem of the seemingly random cells that are checked every 10 cells down or so (which, is not random, it's just that the cell with the checkmark is being reused).
Because cellForRow returns a cached cell you generated. When scrolling out of the screen the order of cells are changed and cells are reused. So it seems "randomly selected".
Don use cellForRow, instead record selection data.
Here's code works in a single view playground.
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController, UITableViewDataSource, UITableViewDelegate {
let tableView = UITableView()
var selection: [IndexPath: Bool] = [:]
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.tableFooterView = UIView()
view.addSubview(tableView)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
tableView.frame = self.view.bounds
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "c")
if let sc = cell {
sc.accessoryType = .none
let isSelected = selection[indexPath] ?? false
sc.accessoryType = isSelected ? .checkmark : .none
return sc
}
return UITableViewCell(style: .default, reuseIdentifier: "c")
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.textLabel?.text = NSNumber(value: indexPath.row).stringValue
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selection[indexPath] = true
tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 30
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
Right now I can add checkmarks to the table of choice selected using your approach vadian. I also added the Boolean("selected") in my data class. What I dont get is how to save the boolean selected for each row with UserDefaults or than how to load this in cell for row at table. Do I need to even touch didload section?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? TableCell else {
return UITableViewCell()
}
let product = products[indexPath.row]
cell.accessoryType = product.selected ? .checkmark : .none
return cell }
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? TableCell else {
return UITableViewCell()
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
var selected = products[indexPath.row].selected
products[indexPath.row].selected = !selected
tableView.reloadRows(at: [indexPath], with: .none)
if selected != true {print("selected", indexPath.row, selected )}
else {print("selected", indexPath.row, selected )}
selected = UserDefaults.standard.bool(forKey: "sound")
}
Add a property var approved : Bool to your data model
Update the property in the model when the selection changes and reload the row.
In cellForRow set the checkmark depending on the property
...
let cell = tableview.dequeueReusableCell(withIdentifier: "myfeedTVC", for: indexPath) as! MyFeedTVC
let user = userDataArray[indexPath.row]
cell.accessoryType = user.approved ? .checkmark : .none
...
I am unable to get the multiple selected rows text into an array by using checkmark searched in stack overflow but unable to implement it can anyone help me how to get the text in an array ?
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return productName.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "filterSelectionCell", for: indexPath) as! FilterSelectionCell
activityIndicator.stopAnimating()
activityIndicator.hidesWhenStopped = true
tableDetails.isHidden = false
cell.brandProductName.text = productName[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
if let cell = tableView.cellForRow(at: indexPath as IndexPath) {
if cell.accessoryType == .checkmark{
cell.accessoryType = .none
}
else{
cell.accessoryType = .checkmark
}
}
}
here is the image for that
Use indexPathsForSelectedRows property of the UITableView
You will be able to get all the indexPaths of selected rows and then integrate over the array of these indexPaths and fetch your texts from your dataset (productName array in your case).
Like this:
fun getAllTextFromTableView() {
guard let indexPaths = self.tableView.indexPathsForSelectedRows else { // if no selected cells just return
return
}
for indexPath in indexPaths {
print("\(productName[indexPath.row])") //Here you get the text of cell
}
}
Of corse you need to have #IBOutlet to your table view in order to access it in the function.
You should use an array of product objects instead of an array of names for your table's data source. Each product would have a name and a Bool value to say whether it's selected or not.
Finding the selected products would then be a easy and -- more important -- you wouldn't be using the UI as your data model to learn whether something was selected.
Here is my answer with the below code you can delete the selected strings from the array which you had appended previously in the array
var values = [String]()
var selected: Bool
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
selected = false
if let cell = tableView.cellForRow(at: indexPath as IndexPath) {
if cell.accessoryType == .checkmark{
cell.accessoryType = .none
print("\(productName[indexPath.row])")
values = values.filter{$0 != "\(productName[indexPath.row])"}
selected = true
print(values)
}
else{
cell.accessoryType = .checkmark
}
}
if selected == true{
print(values)
}
else{
getAllTextFromTableView()
}
}
func getAllTextFromTableView() {
guard let indexPaths = self.tableDetails.indexPathsForSelectedRows else { // if no selected cells just return
return
}
for indexPath in indexPaths {
values.append(productName[indexPath.row])
print(values)
}
}
Trying to develop a checklist app and have been stuck for a while trying to save the state of a checkmark. When I go off the tableView and back on all of the saved checkmarks are erased. I have imported UKIT then defined the class.
Here is my code:
var PreDefinedTasks = ["1", "2", "3", "4"]
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if let cell = tableView.cellForRow(at: indexPath as IndexPath) {
if cell.accessoryType == .checkmark{
cell.accessoryType = .none
}
else{
cell.accessoryType = .checkmark
}
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return PreDefinedTasks.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "List1", for: indexPath)
cell.textLabel?.text = PreDefinedTasks[indexPath.row]
return cell
}
I have looked into NSCoder as a solution but cant seem to get it to work properly. Any help is appreciated!
Here is how I would go about it, if you follow the whole solution, it will save even when the app closes.
Make an array off type Bool like this: var checkmarks = [Int : Bool]()
Then, in cellForRow function, add this:
if checkmarks[indexPath.row] != nil {
cell.accessoryType = checkmarks[indexPath.row] ? .checkmark : .none
} else {
checkmarks[indexPath.row] = false
cell.accessoryType = .none
}
And in the didSelectRow function, add this:
if let cell = tableView.cellForRow(at: indexPath as IndexPath) {
if cell.accessoryType == .checkmark{
cell.accessoryType = .none
checkmarks[indexPath.row] = false
}
else{
cell.accessoryType = .checkmark
checkmarks[indexPath.row] = true
}
}
If you want it to save when the app closes, you have to save the checkmarks array in UserDefaults by doing this:
In didSelectRow function, at the bottom after everything add this:
UserDefaults.standard.set(NSKeyedArchiver.archivedData(withRootObject: checkmarks), forKey: "checkmarks")
UserDefaults.standard.synchronize()
Then also, in viewDidLoad, add this:
if let checks = UserDefaults.standard.value(forKey: "checkmarks") as? NSData {
checkmarks = NSKeyedUnarchiver.unarchiveObject(with: checks as Data) as! [Int : Bool]
}
Let me know if this answer helped and if you have any questions.
EDIT:
So what I completely forgot about is that [Int : Bool] is not an NSDictionary, it is just a Dictionary. UserDefaults can't store Dictionary objects, only NSDictionary which is why you have to change it to NSData so it is able to save the [Int : Bool]. Hope it works this time :)
You can save the indexpath in your array based on checkmark selected on your tableView rows.
As per you implementation, the UITableViewCell is getting reused with identifier as "List1". So if you want to re-use the cell then you have to keep updating the correct accessoryType by storing the state against the predefines tasks.
Since cells get unloaded and later reused, you'll need to store the state of the checkmark elsewhere. In this example in an array called preDefinedTaskCheckmarkState. When loading the cells you'll also need to set the checkmark state.
var PreDefinedTasks = ["1", "2", "3", "4"]
var preDefinedTaskCheckmarkState = [Bool](repeating: false, count: 4)
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if let cell = tableView.cellForRow(at: indexPath as IndexPath) {
preDefinedTaskCheckmarkState[indexPath.row] = !(cell.accessoryType == .checkmark)
cell.accessoryType = preDefinedTaskCheckmarkState[indexPath.row] ? .checkmark : .none
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return PreDefinedTasks.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "List1", for: indexPath)
cell.textLabel?.text = PreDefinedTasks[indexPath.row]
cell.accessoryType = preDefinedTaskCheckmarkState[indexPath.row] ? .checkmark : .none
return cell
}
I created a static TableView and I would like to add or remove a disclosure indicator depending if we are consulting our own account or a guest account.
This is what I would like :
let index = IndexPath(row: 4, section: 0)
let cell = tableView.cellForRow(at: index)
if currentUser {
cell.accessoryType = .none
//cell.backgroundColor = UIColor.red
}
I tried to put it in the viewDidLoad function but it didn't work. I tried cellForRowAt indexPath also and same result.
How could I do that?
Just check if you want to show disclosure indicator in cellForRowAt indexPath method.
if (wantsToShow){ // Your condition goes here
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
}
else{
cell.accessoryType = .none
}
That's it.
Your are working with static cells so cellForRow will not get called. Instead, simply drag connect your cell and set it up, like this
Please use below code in cellForRowTableView code
It will work for you
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
if currentUser {
// Own Account
cell.accessoryType = .none
//cell.backgroundColor = UIColor.red
}else{
//Guest Account
cell.accessoryType =.checkmark
}
}
Swift
Specific cell
if indexPath.row == 1 {
cell.accessoryType = .disclosureIndicator
} else {
cell.accessoryType = .none
}
To add accessory on a specific static cell, I used tableView, cellForRowAt but i couldn't access a reference to UITableViewCell.
Then i found super.tableView(tableView, cellForRowAt: indexPath)
So here is my code:
Assuming you know the specific indexpath you want:
var indexPathSort = IndexPath(item: 0, section: 0)
var indexPathPrice = IndexPath(item: 0, section: 1)
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = super.tableView(tableView, cellForRowAt: indexPath)
if indexPath == indexPathSort || indexPath == indexPathPrice {
cell.accessoryType = .checkmark
}
return cell
}