How to deselect a UITableView Cell when another one is selected? - ios

I want to deselected a table view cell when another cell is selected. Here is the code that's just deselect the cell for second tap:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}

You don't need to call tableView.deselectRow(), just simply set multipleSelection to false.
With Storyboard
Programmatically
tableView.allowsMultipleSelection = false

Select tableView & go to Attributes Inspector & set selection to single selection.
Programatically (inside viewDidLoad() ) you can set as:
self.tableView.allowsSelection = true
self.tableView.allowsMultipleSelection = false

Related

Custom tableview cell not deselecting when I return?

I created a custom table view cell. I added a black transparent overlay image for when the user presses the cell. It works great, however, when you go back to the tableview the cell is still selected. Here is my code.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedCell: PackCell = tableView.cellForRow(at: indexPath)! as! PackCell
selectedCell.highlightImage.isHidden = false
performSegue(withIdentifier: segueIdentifiers[indexPath.row], sender: self)
tableView.deselectRow(at: indexPath, animated: true)
}
Maybe you should also hide your highlightImage when come back?
selectedCell.highlightImage.isHidden = true

afterimage when reloading tableview section in iOS

Hello i'm trying to make drop down menu using tableview and its cells
I use default tableview cell for menu and custom cell for content
custom cell has CollectionView and CollectionView has its cells
drop down and up works fine but it has afterimages like this
this is my code for when menu title cell (default cell) selected
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if indexPath.row == 0 {
sectionDatas[indexPath.section].isOpended = sectionDatas[indexPath.section].isOpended ? false : true
let sections = IndexSet(integer: indexPath.section)
tableView.reloadSections(sections, with: .none)
}
}
is there any solution to remove that afterimages
Try to set clipToBounds = true for collection view or IsHidden - true/false(for collection view)

table view date selected only the long press

I'm using the tableViewCell in Xib.
In table view, didSelectRowAt function is only called when I long press the tableViewCell
My Code is below
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
categorytabelview.deselectRow(at: indexPath, animated: true)
self.categorylabel.text = totalArrayofCars.object(at: indexPath.row) as? String
categorytabelview.isHidden = true
}
I want to select the tabelViewCell data in single select not in long press.
Since you are using gesture, right before adding the gesture to view, add the following line
tap.cancelsTouchesInView = false // assuming tap is your gesture recogniser variable

swift 3 - ViewController not deselecting selected rows in a UITableView

I have a tableview where the user is able to select multiple rows (these rows are distinguished by a checkbox in the row). For some reason, however, I can't implement the functionality to deselect any selected row. Can somebody tell me what I'm missing?
SomeViewController.m
#objc class SomeViewController: UIViewController, NSFetchedResultsControllerDelegate, UITableViewDataSource, UITableViewDelegate {
var deviceArray:[Device] = []
// [perform a fetch]
// [insert fetch results into deviceArray to be displayed in the tableview]
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customcell", for:
indexPath)
// Set up the cell
let device = self.deviceArray[indexPath.row]
cell.textLabel?.text = device.name
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.tableView(tableView, cellForRowAt: indexPath).accessoryType = UITableViewCellAccessoryType.checkmark
NSLog("Selected Row")
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
self.tableView(tableView, cellForRowAt: indexPath).accessoryType = UITableViewCellAccessoryType.none
NSLog("Deselected Row")
}
}
More info:
Looking at the debug logs inserted, I can share the following observations:
When selecting an unselected row, the console prints "Selected Row"
If I click the same row from observation #1, the console prints "Selected Row" only
If I click on any other row, the console prints "Deselected Row" and then "Selected Row"
If I click on the same row as observation #3, the console prints "Selected Row" only.
So, it looks like everytime I click on a different row, tableView: didDeselectRowAt: gets called; however, checkmarks in the clicked row do not go away.
More Info 2:
So I'm new to storyboards and didn't set the "allowsMultipleSelection" property. Going into the Attributes Inspector, this is what my settings look like:
Now, when pressing the same row in the tableView, my console confirms that the app is alternating between tableView:didSelectRowAt: and tableView:didDeselectRowAt:, however, the checkmark doesn't disappear; once the user selects a row, the checkmark remains selected even when tableView:didDeselectRowAt: is called. What else am I missing?
First off make sure your datasource AND delegate outlets are set if you are setting them from storyboard.
Another thing is you need to set allowsMultipleSelection property to true to get the didSelect, didDeselect methods to get called in the behavior you want. Otherwise it will always call didSelect for the cell you tapped on and didDeselect for the most previously selected cell.
The other thing to note is that you are referencing self.tableView when setting your cell.accessoryType property. This may be different instance of the tableView being passed into the delegate method. I recommend a guard let statment to ensure the code setting the accessory type only applies if the tableView being passed into the function. Here is code I used to get it to work.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//Notice I use tableView being passed into func instead of self.tableView
guard let cell = tableView.cellForRow(at: indexPath) else {
return
}
cell.accessoryType = .checkmark
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
guard let cell = tableView.cellForRow(at: indexPath) else {
return
}
cell.accessoryType = .none
}
If you want user to be able to select multiple cells, you need to set tableView.allowsMultipleSelection = true in the viewDidLoad method or set multiple selection in the attributes inspector.

Why do my UITableViewCells turn grey when I tap on them?

When I tap on the cells of my table view, they darken to a grey color, and don't turn back to white until I tap on a different cell. Is there some sort of Boolean I have to set for it to not do that?
Here's a screenshot explaining my problem:
Links to other websites would be helpful, if it would mean a more detailed description. (Unless it's a super simple fix, then the right code or steps-to-take would be easier than a link.)
This is the default behaviour of UITableView.
You must call deselectRowAtIndexPath inside of didSelectRowAtIndexPath inside your UITableViewController class.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
Check out the iOS Documentation for more information.
UITableView
UITableViewDelegate
Swift 3
Option 1: (Which I always use)
To give it fade out animation after selected with gray you can do this:
func tableView(_ tableView: UITableView,
didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
Option 2:
To remove the highlight effect completely you can add this line to your cellForRowAt :
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = .....
cell.selectionStyle = .none
return cell
}
You can do this a couple ways...
tableView.allowsSelection = false
You can set the tableView in xCode Storyboard to not have any selection under the fourth tab.
Or, you can do this on the cell cell.selectionStyle = UITableViewCellSelectionStyle.None
What you want is ultimately going to be about what behavior you are going after. Just do a little experimenting.
Swift 3
In a custom cell add this:
override func awakeFromNib() {
super.awakeFromNib()
selectionStyle = .none
}
This ensures you won't even see the gray when the cell is tapped. This code in the UITableViewDelegate only deselects when tapped.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
Just Simply click on the cell and go to attributes inspector you will find Selection Style , select none.
You can change style by:
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
Swift 4.1
tableView.deselectRow(at: indexPath, animated: true)
In the storyboard or xib,
In the Tableview cell,
you can select selection to "none",
in the atributes inspector

Resources