Hide button in another cell when cell is tapped - swift

I have a `tableView with three cells. In two of them I have a button. I would like to hide the button in the other cell, when one of the cells is tapped.
I have tried some solutions in didSelectRowAt but have not been able to hide the buttons:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 1 {
if let cell = tableView.cellForRow(at: indexPath) as? ComposeCell1 {
cell.compose.isHidden = true
if let cell2 = tableView.cellForRow(at: indexPath) as? ComposeCell2 {
cell2.compose.isHidden = true //I can reach the cell from here, but its not hiding the button
}
}
} else if indexPath.row == 2 {
if let cell = tableView.cellForRow(at: indexPath) as? ComposeCell2 {
cell.compose.isHidden = true
}
}
}

Try to track the index for the pressed button with a variable. And use the variable in tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell to update the button's visibility. Only thing you need to do in didSelectRowAt is assign the index value the variable and reload the table view
var selectedIndex: Int?
....
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedIndex = indexPath.row
tableView.reloadData()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
.....
cell.compose.isHidden = indexPath.row != selectedIndex
.....
return cell
}

Related

How to keep checkmarks from disappearing from TableView when scrolling

I currently have a tableView that gets it data from a API that outputs JSON and allows the user to tap to add a checkmark accessory to the right side. The issue I am having is after tapping on a cell in a long list of records and scrolling that row out of view, the checkmark disappears.
This is what I am doing, but when scrolling the checkmarks still disappear when off screen:
var selectedIndexPaths: Set<String> = []
override func tableView(_ tableView: UITableView, cellForRowAt
indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "testcell",
for: indexPath)
if selectedIndexPaths.contains(indexPath.description) {
cell.accessoryType = .checkmark
} else {
cell.accessoryType = .none
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath:
IndexPath) {
selectedIndexPaths.insert(indexPath.description)
tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
}
override func tableView(_ tableView: UITableView, didDeselectRowAt
indexPath: IndexPath) {
selectedIndexPaths.remove(indexPath.description)
tableView.cellForRow(at: indexPath)?.accessoryType = .none
}
Why is the issue still persisting?

UITableView checkmark hide when scrolling

When I scroll the screen, the upper markings disappear. I've tried some solutions found here, but none worked ...
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
cell.textLabel?.text = players[indexPath.row]
cell.textLabel?.textColor = UIColor.white
cell.backgroundColor = colorForIndex(index: indexPath.row)
cell.textLabel?.font = UIFont(name: "Chalkboard SE", size: 20)
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.tableView.deselectRow(at: indexPath, animated: true)
if let cell = tableView.cellForRow(at: indexPath){
if (cell.accessoryType == .none) {
cell.accessoryType = .checkmark
let player = players[indexPath.row]
playersSelecteds.append(player)
} else {
cell.accessoryType = .none
let player = players[indexPath.row]
if let position = playersSelecteds.index(of: player) {
playersSelecteds.remove(at: position)
}
}
}
}
You should have a model for check-marked cells. because every time you scroll, the cells being reload based on your tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell method.
So, for example add a model like this:
var playersCheckmarked: [Bool] = []
Then add in viewDidLoad some boolean to this array as many as players array have objects. Next in override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) method set playersCheckmarked[indexPath.row] = true or playersCheckmarked[indexPath.row] = false based on user selection.
And of course in override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell method add this line:
cell.accessoryType = playersCheckmarked[indexPath.row] == true .checkmark : .none

how to didSelectRowAt in multiple cell?

I will paste some of my code
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if menu == "your_products"{
let cell = tableView.dequeueReusableCell(withIdentifier: "VendorTableViewCellId", for: indexPath as IndexPath) as! VendorTableViewCell
let vendor = self.vendors![indexPath.row]
cell.setVendor(vendor: vendor)
return cell
}else{
let cell = tableView.dequeueReusableCell(withIdentifier: "EventTableViewCellId", for: indexPath as IndexPath) as! EventTableViewCell
cell.setEvent(event: self.events![indexPath.row])
return cell
}
}
how to access each section and each row through last function didSelectRowAtIndexPath using indexPath.section and indexPath.row in multiple cell?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.tableView.deselectRow(at: indexPath as IndexPath, animated: false)
print("You tapped cell number \(indexPath.row).")
}
Remove this line self.tableView.deselectRow(at: indexPath as IndexPath, animated: false)
#objc func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
print(indexPath.row)
print(indexPath.section)
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("You tapped cell number \(indexPath.row).")
}
I think it should work.
If this is not working then please check your delegates are connected or not.

Cell does not get deselected while scrolled back

I have a weird problem. While I select a cell with changing the background color of one UIView component, then I scroll down and selected cell going to be out of bounds of the view. I do select new cell -> previous one should get unselected, but it does not. Because when I back I do have 2 selected cells.
var lastSelectedAtIndexPath: IndexPath?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch tableView {
case myTableView:
if let cell = myTableView.cellForRow(at: indexPath) as? MyTableViewCell {
cell.checkMarkBorder.backgroundColor = UIColor.darkGreyFts
lastSelectedFuelAtIndexPath = indexPath
}
default:
break
}
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
switch tableView {
case myTableView:
if let cell = fuelTableView.cellForRow(at: indexPath) as? MyTableViewCell {
cell.checkMarkBorder.backgroundColor = UIColor.white
}
default:
break
}
}
And iside cellForRowAt I have :
let cell = myTableView.dequeueReusableCell(withIdentifier: "myCell") as! MyTableViewCell
if let lastIndexPath = lastSelectedFuelAtIndexPath {
myTableView.deselectRow(at: lastIndexPath, animated: true)
}
cell.myImage.image = UIImage(named: fuelType)?.withRenderingMode(.alwaysTemplate)
cell.myNameLabel.text = "Test"
Any ideas what is going on?
Thanks in advance!
Don't make changes on cell outside cellForRowAt always change your dataSource and use dataSource in cellForRowAt then in didSelectRowAt and didDeSelectRowAt reload the row.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = myTableView.dequeueReusableCell(withIdentifier: "myCell") as! MyTableViewCell
cell.myImage.image = UIImage(named: fuelType)?.withRenderingMode(.alwaysTemplate)
cell.myNameLabel.text = "Test"
cell.checkMarkBorder.backgroundColor = lastSelectedFuelAtIndexPath == indexPath ? UIColor.darkGreyFts : UIColor.white
return cell
}
Now in didSelectRowAt and didDeSelectRowAt update the lastSelectedFuelAtIndexPath reload the row.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView == myTableView {
lastSelectedFuelAtIndexPath = indexPath
myTableView.reloadRows(at: [indexPath], with: .automatic)
}
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if tableView == myTableView {
lastSelectedFuelAtIndexPath = nil
myTableView.reloadRows(at: [indexPath], with: .automatic)
}
}

tableview persisting cell image after reloading

I have a tableview with cells that when selected displays an image within the selected cell, this image then disappears when the cell is selected again, and so on. When i press a submit button the selected cells are remembered and the tableview reloads with new data. But when doing this all the new data loads but the selected cell image persists. I have tried calling tableView.reloadData() on the main queue but it still persists. The image also persists when i press the submit button several times.
Heres my code:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return currentQuestion.answers.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return tableView.frame.height/CGFloat(currentQuestion.answers.count)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
setSelectedArray()
let cell: AnswersTableViewCell = tableView.dequeueReusableCell(withIdentifier: "answersTableViewCell") as! AnswersTableViewCell
let text = currentQuestion.answers[indexPath.row]
let isAnAnswer = currentQuestion.answerKeys[indexPath.row]
cell.answerTextLabel.text = text
cell.answerView.backgroundColor = UIColor.white.withAlphaComponent(0.5)
cell.contentView.sendSubview(toBack: cell.answerView)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell: AnswersTableViewCell = tableView.cellForRow(at: indexPath) as? AnswersTableViewCell {
if cell.answerImageView.image == nil {
cell.answerImageView.image = UIImage(named: "paw.png")
selected[indexPath.row] = true
} else {
cell.answerImageView.image = nil
selected[indexPath.row] = false
}
}
}
#IBAction func submitButtonWasPressed() {
if questionNumber < questions.count - 1 {
questionNumber += 1
setCurrentQuestion()
self.answersTableView.reloadData()
self.view.setNeedsDisplay()
}
}
Any help would be great. Thanks
You need to set the image back to its correct value in cellForRow. Cells in your table are reused between calls to reloadData, and since you're not touching the imageView, it's keeping its previous value. Looks to me like you want:
cell.answerImageView.image = selected[indexPath.row] ? UIImage(named: "paw.png") : nil
inside of tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath).