Swift tableview cell doesn't save the state with different identifier - swift

I am very confused on the reuse of the cells.
I have a table, each cell is a cell with a switch on it. If I toggle a switch I set the background color of that cell to a different color. However every time I scroll these changes don't persist.
I am subclassing UITalbeViewCell to create my own custom cell. Each cell has a different identifier. However when I scroll through the table, whatever changes I made to the cell still doesn't save. I've read similar questions but none of them worked.. Some suggested subclass which I did, some suggested use different identifier which I also did...
Here is the code of my tableview.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let key = Array(dataSource[indexPath.section].keys)[indexPath.row]
let cell = CellWithSwitch.init(style: .subtitle, reuseIdentifier: key)
cell.awakeFromNib()
let val = Array(dataSource[indexPath.section].values)[indexPath.row]
cell.switchView?.addTarget(self, action: #selector(self.switchChanged(_:)), for: .valueChanged)
if let index = key.firstIndex(of: "."){
cell.textLabel?.text = String(key.suffix(from: key.index(index, offsetBy: 1)))
}else{
cell.textLabel?.text = key;
}
cell.switchView?.setOn(val, animated: true)
return cell
}

You can change array value in switchChange action
lets i take array for switch as below:
var arrSwitch = [false,false,false,false,false,false,false,false,false,false]
Below is my cellForRowAt Method
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customCell") as! customCell
cell. switchView.setOn(self.arrSwitch[indexPath.row], animated: false)
cell. switchView.tag = indexPath.row
cell. switchView.addTarget(self, action: #selector(self.onSwitchTap(_:)), for: .valueChanged)
return cell
}
Here is my onSwitchTap Action
#IBAction func onSwitchTap(_ sender: UISwitch) {
self.arrSwitch[sender.tag] = !self.arrSwitch[sender.tag]
}
Now on scroll it will persist last changes you have done.

Related

<Swift>How to navigate to a new page in custom cell?

I have a custom cell FeedCell.swift and the controller FeedViewController.swift. And I set a btn in each cell so that I can navigate to detailController.swift. But in custom cell I can't use self.navigation. What should I do?
You can set an outlet of Button in cell and addTarget it like :
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellID") as! yourCell
cell.yourButton.addTarget(self, action: #selector(goDetail), for: .touchUpInside)
return cell
}
#objc func goDetail(){
let vc = // Declare your viewController
self.navigationController?.pushViewController(vc, animated:true)
}
Btw you can add this action when a cell selected instead of using button
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
let vc = // Declare your viewController
self.navigationController?.pushViewController(vc, animated:true)
}
If you want to send which cell's button clicked just add cell.yourButton.tag = indexPath.row in cellForRowAt and modify goDetail with
#objc func goDetail(sender : UIButton){
sender.tag // while give your selected cell index
}

Crash: Attempted to dequeue multiple cells for the same index path, which is not allowed

I have tableview datasource func to build a cell from a factory method function.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return OWTableViewOrganizer.instance.configureCell(at: indexPath)!
}
The factory method is here:
func configureCell(at indexPath: IndexPath) -> UITableViewCell? {
var cell = UITableViewCell()
switch indexPath.section {
case thisWorkoutSections.barbel.sectionNumber():
cell = barebellCell(indexPath: indexPath)
break
case thisWorkoutSections.lastWorkout.sectionNumber():
cell = lastWorkoutCell(indexPath: indexPath)
break
case thisWorkoutSections.personalRecord.sectionNumber():
cell = personalRecordCell(indexPath: indexPath)
break
case thisWorkoutSections.notes.sectionNumber():
break
default:
break
}
return cell
}
I have this code to build the cell:
func lastWorkoutCell(indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: WorkoutSetTableViewCell.cellIdentifier(), for: indexPath) as! WorkoutSetTableViewCell
if OWTableViewOrganizer.instance.lastWorkoutExerciseSets.count > 0 {
if indexPath.row < OWTableViewOrganizer.instance.lastWorkoutExerciseSets.count {
let logExerciseSet = OWTableViewOrganizer.instance.lastWorkoutExerciseSets[indexPath.row]
let setNumber = indexPath.row + 1
if let weight = logExerciseSet.weight?.doubleValue, let reps = logExerciseSet.reps?.intValue {
cell.setupCellWithData(setNumber: setNumber, weight: weight, reps: reps)
}
} else {
cell.setupCellWithData(setNumber: -1, weight: 0, reps: 0)
}
} else {
cell.setupCellWithData(setNumber: -1, weight: 0, reps: 0)
}
return cell
}
But time to time this line crashes for me:
let cell = tableView.dequeueReusableCell(withIdentifier: WorkoutSetTableViewCell.cellIdentifier(), for: indexPath) as! WorkoutSetTableViewCell
With error:
Attempted to dequeue multiple cells for the same index path, which is not allowed. If you really need to dequeue more cells than the table view is requesting, use the -dequeueReusableCellWithIdentifier: method (without an index path)
I know code style and design is not ideal here, please skip this if you have comments.
I don't know where to look, I tried simply remove indexPath, but it looks does not help or bring even more issues:
let cell = tableView.dequeueReusableCell(withIdentifier: WorkoutSetTableViewCell.cellIdentifier()) as! WorkoutSetTableViewCell
I have one controller which presents another one at the top of it (like in Apple music) and I can swipe down to show bottom controller and swipe up to bring back top controller. I noticed in log that I have some presentation alert, not sure if this something I need to deal with to resolve the issue above but JFY info.
It seems like there are two table view trigger
tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
and you only dequeue cell from 1 table view at
let cell = tableView.dequeueReusableCell(withIdentifier: WorkoutSetTableViewCell.cellIdentifier(), for: indexPath) as! WorkoutSetTableViewCell
You should try to pass tableView from
tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
to
lastWorkoutCell(indexPath: IndexPath)
(which will become lastWorkoutCell(indexPath: IndexPath, tableView: UITableView)) and dequeue cell from tableView

How to change the state of the image in cell made in xib?

There is a TableView and a "Locked" image, how do I get alpha = 0 when I click cell 1 in cell 2? There is the cellForRowAt function:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath) as! TableViewCell
cell.commonInit("got_\(indexPath.item)", title: titleLessons[indexPath.item], sub: summary[indexPath.item], imageLock: imageZ[indexPath.item])
return cell
}
There is the didSelectRowAt function:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if unlockedRows.contains(indexPath.row) {
let nextIndex = indexPath.row + 1
if nextIndex < pdfArr.count {
let vc = DetailLessonsVC()
let ce = TableViewCell()
vc.pdfTitle = pdfArr[indexPath.row]
vc.numberList = pdfArr[indexPath.row]
vc.linkList = link[indexPath.row]
unlockedRows.append(indexPath.row + 1)
self.navigationController?.pushViewController(vc, animated: true)
self.tableView.deselectRow(at: indexPath, animated: true)
}
print("no")
}
}
TableViewCell is xib's custom class.
You need to access the cell, and change image property, you can access the cell in didselect method with this:
let cell = tableView.cellForRowAtIndexPath(indexPath)
In your cell's custom class (TableViewCell) create an outlet for your image. If I understand correctly you want to edit cell located in the row number N+1 when user taps on the cell located in the row above, right?
If that's the case then all you have to do is get a reference to the next cell in didSelectRowAt. You can do just that this way:
var nextCellsIndexPath = indexPath
nextCellsIndexPath.row += 1
// Use 'if let' in order to make sure the cell at that index path exists
if let nexCell = tableView.cellForRow(at: nextCellsIndexPath) {
nextCell.lockedImage.alpha = 0
}
Since you didn't specify the number of sections in your table view, I assumed it has just one.

Swift TableView multiple Custom cells, inrease number of row in from cell lead to crash

I have problem which trying to solve for hours.
I have tableView with multiple cells.
I don't use StoryBoard.
My problem is this:
Number of cells in tableView depends on size of array. When app start, tableView and its cells loads perfectly. Even when i insert into ViewDidLoad this:
arrayOfCategory.append(Category(nameOfCategory: "Example", ColorOfCategory: .white))
tableView.reloadData()
it works. But when i try insert voids into button's target
#objc func handle(sender: UIButton) {
arrayOfCategory.append(Category(nameOfCategory: "Sdsdd", ColorOfCategory: .white))
tableView.reloadData()
}
my app will crash with error:
TaskList[59513:1886772] Could not cast value of type 'TaskList.SwitchCell' (0x1073bd808) to 'TaskList.CategoryShowCell' (0x1073bd500).
Here is cellForRow:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.section {
case 0:
tableView.register(CategoryShowCell.self, forCellReuseIdentifier: categoryShowID)
let cell = tableView.dequeueReusableCell(withIdentifier: categoryShowID, for: indexPath) as! CategoryShowCell
cell.setCell(category: Category(nameOfCategory: "sdsd", ColorOfCategory: .white))
return cell
case 1:
tableView.register(SwitchCell.self, forCellReuseIdentifier: swichCellID)
let cell = tableView.dequeueReusableCell(withIdentifier: swichCellID, for: indexPath) as! SwitchCell
return cell
default:
tableView.register(SwitchCell.self, forCellReuseIdentifier: swichCellID)
let cell = tableView.dequeueReusableCell(withIdentifier: swichCellID, for: indexPath) as! SwitchCell
return cell
}
}
Does anyone know where is problem?
Xcode point into this line:
let cell = tableView.dequeueReusableCell(withIdentifier: categoryShowID, for: indexPath) as! CategoryShowCell
in cellForRow bude one line above i register right cell.
Your problem is in different types of registered cell and type you're trying to force cast it to. Like Xcode says you - TaskList.SwitchCell to TaskList.CategoryShowCell. Force casting happens at as! CategoryShowCell this place.
I suspect, that you are reregistering SwitchCell.self for categoryShowID. It can happen if your categoryShowID is equal to swichCellID.
Check this out and keep in mind, that registering one cell multiple times in cellForRow is a bad idea

Swift didDeselectRowAtIndexPath not working on preset cell

I have a table I use that shows timezone options. I use a checkmark to show which one is currently selected. When the table is created I checkmark the cell that is saved as the users timezone.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cellData")
switch indexPath.row {
case 0: cell.textLabel?.text = "Eastern"
case 1: cell.textLabel?.text = "Central"
case 2: cell.textLabel?.text = "Mountain"
case 3: cell.textLabel?.text = "Mountain (No DST)"
case 4: cell.textLabel?.text = "Pacific"
default: cell.textLabel?.text = ""
}
cell.selectionStyle = UITableViewCellSelectionStyle.None
if(cell.textLabel?.text == keychain.get("timezone")) {
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
}
return cell
}
Then I use these functions to change the checkmark when a user chooses a new timezone.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.cellForRowAtIndexPath(indexPath)!.accessoryType = UITableViewCellAccessoryType.Checkmark
}
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
tableView.cellForRowAtIndexPath(indexPath)!.accessoryType = UITableViewCellAccessoryType.None
}
However, when I preset the checkmark it is not removed when I choose a new timezone. It will only work if I first select it and then choose a new one. Is there a reason the original cell is not affected on deselect?
The reason your original cell isn't being deselected is that it's not selected in the first place. Enabling the checkmark accessory doesn't select the cell.
It's a bit of a pain to set up, but one way you could get this working is by storing a reference to the cell that needs to be selected, and when the view is going to appear, select it manually. Then, deselections will work.
Add a class variable to remember the cell that should be selected
var initiallySelectedPath: NSIndexPath?
Set the variable in your cellForRowAtIndexPath (personally, I'd do this setting elsewhere in the class due to how the actual cell selection is going to be performed, but this is good enough to demonstrate a solution.
...
if (cell.textLabel?.text == keychain.get("timezone")) {
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
initiallySelectedPath = indexPath
}
...
Then, in your viewWillAppear
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if let indexPath = initiallySelectedPath {
// I force unwrap (sorry!) my tableView, you'll need to change this to however you reference yours
tableView!.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: UITableViewScrollPosition.None)
}
}
Now your original cell should deselect with the first tap on a different cell.