Disclosure indicator to specific cell in static TableView in Swift - ios

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
}

Related

Checkmark changes after scrolling in table view

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
}

How do I save checkmarks that disappear when scrolling in UITableView?

I'm pretty new to Swift and I'm working on my first app. I'm currently using the UITableView that has an option for checkmarks to appear on the right when users tap on it. It works fine but whenever you scroll down on the list of items, the checkmarks disappear. I've checked a few online sources but I'm unsure of how to apply it to the code I have. Any help would be greatly appreciated!
Also, is there any way that I can store the checkmarks for when the user reopens the app? Every time I restart the app, the list of checks resets.
Here is the code I have so far:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "celltwo")
cell.textLabel?.text = list[indexPath.row]
return(cell)
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView.cellForRow(at: indexPath)?.accessoryType == UITableViewCellAccessoryType.checkmark
{
tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.none
}
else
{
tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark
}
tableView.deselectRow(at: indexPath, animated: true)
}
The checkmarks are disappearing on scrolling because table views reuse the cells so the 'cellForRowAt' method gets called whenever you scroll and you haven't provided the logic to show/hide the checkmark in this method. To solve this you can do the following,
Initialise an Array to store the indexes of the selected cells.
var selectedIndexes : [Int] = []
Update your 'didSelectRowAt' method with the logic to add/remove indexes to/from the 'selectedIndexes' array.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView.cellForRow(at: indexPath)?.accessoryType == UITableViewCellAccessoryType.checkmark
{
tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.none
let indexOfItemToRemove = self.selectedIndexes.index(of: list[indexPath.row])
self.selectedIndexes.remove(at: indexOfItemToRemove)
}
else
{
tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark
self.selectedIndexes.append(indexPath.row)
}
}
Update your 'cellForRowAt' method with the logic to show/hide checkmark.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "celltwo")
cell.textLabel?.text = list[indexPath.row]
if self.selectedIndexes.contains(indexPath.row)
{
//cell was selected earlier
cell.accessoryType = UITableViewCellAccessoryType.checkmark
}else
{
// cell was not selected earlier
cell.accessoryType = UITableViewCellAccessoryType.none
}
return cell
}
In order to save the selection for the next time the app is launched you could save the 'selectedIndexes' array to UserDefaults. In order to achieve this do the following :
Update the 'didSelectRowAt' method to include the logic to save the selected index to UserDefaults. At the following code at the end of the method.
let userDefaults = UserDefaults.standard
userDefaults.set(selectedIndexes, forKey: "SelectedIndexes")
Add the following code to the 'viewDidLoad' method.
let userDefaults = UserDefaults.standard
self.selectedIndexes = userDefaults.value(forKey: "SelectedIndexes")
The cell accessoryType disappears because the reusability feature the UITableView use, in order to keep the selection follow the following code:
override func viewDidLoad()
{
super.viewDidLoad()
tableView.allowsMultipleSelection = true
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "celltwo")
cell.textLabel?.text = list[indexPath.row]
if let paths = tableView.indexPathsForSelectedRows
{
if (paths.contains(indexPath))
{
cell.accessoryType = .checkmark
}
else
{
cell.accessoryType = .none
}
}
else
{
cell.accessoryType = .none
}
cell.selectionStyle = .none
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
if tableView.cellForRow(at: indexPath)?.accessoryType == UITableViewCellAccessoryType.checkmark
{
tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.none
}
else
{
tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark
}
//remove deselection
//tableView.deselectRow(at: indexPath, animated: true)
}
Regard saving the selection, this info I guess should be persisted with your datasource in CoreData or UserDefaults.

TableViewCell scroll selects wrong cells when scrolling

My tableview highlights the wrong cells when I scroll up and down. In didSelectRowAtIndexPath, I'm trying to add the .Checkmark accessory view as well as change the font of cell.textLabel?.text. Upon scrolling, random rows are selected and the ones I manually select sometimes deselect.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) as! MyCell
cell.textLabel?.text = self.array[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var cell: UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
if cell.accessoryType == UITableViewCellAccessoryType.Checkmark {
cell.accessoryType = .None
cell.textLabel!.font = UIFont(name: "HelveticaNeue", size: 16)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
} else if cell.accessoryType == UITableViewCellAccessoryType.None {
cell.accessoryType = .Checkmark
cell.textLabel!.font = UIFont(name: "HelveticaNeue-Bold", size: 16)
}
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
Declare array which will keep track of which rows are selected:
var selectedRows:[Bool] = []
You need to make sure that it is populated with the same number of items as your data array.
Then modify your cellForRowAtIndexPath:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) as! MyCell
cell.textLabel?.text = self.array[indexPath.row]
if selectedRows[indexPath.row] {
// row selected
cell.accessoryType = .Checkmark
}
else {
cell.accessoryType = .None
}
return cell
}
And in your didSelectRow function add line:
selectedRows[indexPath.row] = !selectedRows[indexPath.row]
When you modify your cell from didSelectRow the next time cell is dequeued it will have the accessory type which you set. The reason why it happens after scroll is because when you scroll the cells which become visible get dequeued. So, you need to manually set correct accessory type for them

UI table view cell check marks repeating swift

My when a table cell is checked and you scroll down a check mark is repeated.
I know this is due to cell reuse, but don't know how to fix it.
function to populate table
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let item = self.myEvents[indexPath.row]
let cell = tableView.dequeueReusableCellWithIdentifier("row", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel!.text = self.myEvents[indexPath.row]
return cell
}
//function to make the table checkable
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("indexpath: \(indexPath)")
let cell: UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
let text = cell.textLabel!.text
if cell.accessoryType == UITableViewCellAccessoryType.None {
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
//let text = cell.textLabel!.text
if(checkedEvents[0] == ""){
checkedEvents[0] = text!
}else{
checkedEvents.append(text!)
}
} else {
cell.accessoryType = UITableViewCellAccessoryType.None
var index = 0
for event in checkedEvents{
if event == text{
self.checkedEvents.removeAtIndex(index)
index++
}
}
}
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
First, you need to store the number of the selected row somewhere. How about self.selectedRowNumber?
var selectedRowNumber: Int? = nil
Set this when the user selects a row (short version):
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell: UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
cell.accessoryType = .Checkmark
self.selectedRowNumber = indexPath.row
// You'll also need some code here to loop through all the other visible cells and remove the checkmark from any cells that aren't this one.
}
Now modify your -tableView:cellForRowAtIndexPath: method to clear the accessory if it's not the selected row, or add it if it is:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("row", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel!.text = self.myEvents[indexPath.row]
cell.accessoryType = .None
if let selectedRowNumber = self.selectedRowNumber {
if indexPath.row == selectedRowNumber {
cell.accessoryType = .Checkmark
}
}
return cell
}
This code was written here in the browser, and may need some fixes to compile.
If you want only one selection, put tableView.reloadData() in your didSelectRowAtIndexPath function

UITableView Checkmark ONLY ONE Row at a Time

With this code, I can check multiple rows in a table.
But what I want is to only have one row checked at a time. Here's my code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *theCell = [tableView cellForRowAtIndexPath:indexPath];
if (theCell.accessoryType == UITableViewCellAccessoryNone) {
theCell.accessoryType = UITableViewCellAccessoryCheckmark;
} else if (theCell.accessoryType == UITableViewCellAccessoryCheckmark) {
theCell.accessoryType = UITableViewCellAccessoryNone;
}
}
If a user selects a different row, then I want the old row to simply automatically uncheck. Don't know how to do that.
Objective-C:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}
Swift:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
tableView.cellForRow(at: indexPath)?.accessoryType = .none
}
You can create a new variable to track the index selected at didSelectRowAtIndex.
int selectedIndex;
in your cellForRowAtIndexPath
if(indexPath.row == selectedIndex)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
and in your didSelectRowAtIndex
selectedIndex = indexPath.row;
[tableView reloadData];
The best way is to set the accessory type in cellForRowAtIndexPath.
Use didSelectRowAtIndexPath to only record which path should be selected and then call reloadData.
You don't need to (and shouldn't) just reload the table after each selection.
Apple has good documentation on how to manage a selection list. See Listing 6-3 for an example.
This is more or less the same answer as some of the others but I thought I'd add a little more detail.
What you want to do is save the current selected IndexPath to a variable and use that in didSelectRowAtIndexPath to remove the old check mark. This is also where you will be adding the new check mark.
You need to make sure to also set/unset the checkmarks in cellForRowAtIndexPath otherwise if your list is large and cells are reused it will look like multiple rows are selected. This is a problem with some of the other answers.
See swift 2.0 example below:
// for saving currently seletcted index path
var selectedIndexPath: NSIndexPath? = NSIndexPath(forRow: 0, inSection: 0) // you wouldn't normally initialize it here, this is just so this code snip works
// likely you would set this during cellForRowAtIndexPath when you dequeue the cell that matches a saved user selection or the default
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// this gets rid of the grey row selection. You can add the delegate didDeselectRowAtIndexPath if you want something to happen on deselection
tableView.deselectRowAtIndexPath(indexPath, animated: true) // animated to true makes the grey fade out, set to false for it to immediately go away
// if they are selecting the same row again, there is nothing to do, just keep it checked
if indexPath == selectedIndexPath {
return
}
// toggle old one off and the new one on
let newCell = tableView.cellForRowAtIndexPath(indexPath)
if newCell?.accessoryType == UITableViewCellAccessoryType.None {
newCell?.accessoryType = UITableViewCellAccessoryType.Checkmark
}
let oldCell = tableView.cellForRowAtIndexPath(selectedIndexPath!)
if oldCell?.accessoryType == UITableViewCellAccessoryType.Checkmark {
oldCell?.accessoryType = UITableViewCellAccessoryType.None
}
selectedIndexPath = indexPath // save the selected index path
// do whatever else you need to do upon a new selection
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
// if this is the currently selected indexPath, set the checkmark, otherwise remove it
if indexPath == selectedIndexPath {
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
} else {
cell.accessoryType = UITableViewCellAccessoryType.None
}
}
You don't need to reload the tableView.
See the below code:
Declare a NSIndexPath lastSelectedIndexPath variable for your class
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(lastSelectedIndexPath) {
UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:lastSelectedIndexPath];
[lastCell setAccessoryView:nil];
}
UITableViewCell *currentCell = [tableView cellForRowAtIndexPath:currentIndexPath];
[currentCell setAccessoryView:UITableViewCellAccessoryCheckmark];
lastSelectedIndexPath = indexPath;
}
For Swift 3 following worked for me :
override func viewDidLoad() {
super.viewDidLoad()
// allow cells to be selected
tableView.allowsSelection = true
// ensure that deselect is called on all other cells when a cell is selected
tableView.allowsMultipleSelection = false
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableVIew.cellForRow(at: indexPath as IndexPath)?.accessoryType = .checkmark
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
tableVIew.cellForRow(at: indexPath as IndexPath)?.accessoryType = .none
}
I think https://stackoverflow.com/a/5960016/928599 will help you.
I used it and it Works with deselectRowAtIndexPath too!
Simplest way is to save the selected IndexPath and check it in cellForRowAtIndexPath:
Initialize the selected index path:
selectedIndexPath = [[NSIndexPath alloc] init];
In cellForRowAtIndexPath:
if([selectedIndexPath isEqual:indexPath]){
[checkMark setHidden:NO];
} else {
[checkMark setHidden:YES];
}
In didSelectRowAtIndexPath:
selectedIndexPath = indexPath;
[tableView reloadData];
It should work work try this Njoy :)
Try this:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.section == 1 {
// un-select the older row
if let selected = self.LastSelected {
tableView.cellForRowAtIndexPath(selected)?.accessoryType = .None
}
// select the new row
tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = UITableViewCellAccessoryType.Checkmark
self.LastSelected = indexPath
}
}
In Swift, following works perfectly:
lastSelectedIndexPath = NSIndexPath(forRow: 1, inSection: 0)//Row will be the previous selected row
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell:LabelsSelectionCell = tableView.dequeueReusableCellWithIdentifier("LabelsSelectionCell", forIndexPath: indexPath) as! LabelsSelectionCell
cell.setCellLael(labelOptionsArray[indexPath.row])
if lastSelectedIndexPath == indexPath
{
cell.setCellCheckedStatus(true)
}
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
if let _ = lastSelectedIndexPath
{
let lastCell:LabelsSelectionCell = tableView.cellForRowAtIndexPath(lastSelectedIndexPath!) as! LabelsSelectionCell
lastCell.setCellCheckedStatus(false)
}
let currentCell:LabelsSelectionCell = tableView.cellForRowAtIndexPath(indexPath) as! LabelsSelectionCell
currentCell.setCellCheckedStatus(true)
lastSelectedIndexPath = indexPath
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
In Swift 2.0 I used this:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if((lastSelectedIndexPath) != nil) {
let lastCell = tableView.cellForRowAtIndexPath(lastSelectedIndexPath)
lastCell?.accessoryType = UITableViewCellAccessoryType.None
}
let currentCell = tableView.cellForRowAtIndexPath(indexPath)
currentCell?.accessoryType = UITableViewCellAccessoryType.Checkmark
lastSelectedIndexPath = indexPath
}
My way is deselect other cells during selecting:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = ....
if condition {
cell.accessoryType = .checkmark
tableView.selectRow(at: indexPath, animated: false, scrollPosition: UITableViewScrollPosition.bottom)
} else {
cell.accessoryType = .none
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
for row in 0...tableView.numberOfRows(inSection: 0) {
if row == indexPath.row {continue}
tableView.deselectRow(at: IndexPath(row: row, section: 0), animated: true)
}
if let cell = tableView.cellForRow(at: indexPath) {
cell.accessoryType = .checkmark
}
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
cell?.accessoryType = .none
}
Here's what I came up when I had this problem.
This code is implemented in the latest version of Swift, as of today...
For more info and/or the extension file, please check out the Github Gist of this snippet.
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let sip = selectedIndex {
tableView.deselectRow(at: sip, animated: false)
}
selectedIndex = indexPath
}
override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if selectedIndex?.row == indexPath.row {
selectedIndex = nil
}
}
tested and solved try this its worked perfectly
add this as global variable
var selectedIndexPath = NSIndexPath(row: 0, section: 0)
Add this in didselect method
// old one check off
if indexPath != selectedIndexPath as IndexPath {
let oldCell = categorytable.cellForRow(at: selectedIndexPath as IndexPath)
if oldCell?.accessoryView != nil {
oldCell?.accessoryView = nil
}
else {
let imageName = "checkmark"
let image: UIImageView = UIImageView(image: UIImage(named: imageName))
cell.accessoryView = image
}
}
// the new one on
if cell.accessoryView == nil {
let imageName = "checkmark"
let image: UIImageView = UIImageView(image: UIImage(named: imageName))
cell.accessoryView = image
}
else {
cell.accessoryView = nil
}
You can do it in custom cell type in one line of code.
final class CheckableTableViewCell: UITableViewCell {
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
self.accessoryType = selected ? .checkmark : .none
}
}
To make this method work - your cell should be selected.
If you don’t want to see the highlight of the selected cell, just set cell's selectionStyle to .none in storyboard or in code
This method will not work, if you will call
tableView.deselectRow(at: indexPath, animated: true)
Also works great with multiple selection.
Swift 5 version of jeffjv's response.
// for saving currently selected index path
var selectedIndexPath: NSIndexPath? = NSIndexPath(forRow: 0, inSection: 0) // you wouldn't normally initialize it here, this is just so this code snip works
// likely you would set this during cellForRowAtIndexPath when you dequeue the cell that matches a saved user selection or the default
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) {
// this gets rid of the grey row selection. You can add the delegate didDeselectRowAtIndexPath if you want something to happen on deselection
tableView.deselectRow(at: indexPath, animated: true) // animated to true makes the grey fade out, set to false for it to immediately go away
// if they are selecting the same row again, there is nothing to do, just keep it checked
if indexPath == selectedIndexPath! as IndexPath {
return
}
// toggle old one off and the new one on
let newCell = tableView.cellForRow(at: indexPath)
if newCell?.accessoryType == UITableViewCell.AccessoryType.None {
newCell?.accessoryType = UITableViewCell.AccessoryType.Checkmark
}
let oldCell = tableView.cellForRow(at: selectedIndexPath! as IndexPath)
if oldCell?.accessoryType == UITableViewCell.AccessoryType.Checkmark {
oldCell?.accessoryType = UITableViewCell.AccessoryType.None
}
selectedIndexPath = indexPath as NSIndexPath // save the selected index path
// do whatever else you need to do upon a new selection
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
// if this is the currently selected indexPath, set the checkmark, otherwise remove it
if indexPath == selectedIndexPath! as IndexPath {
cell.accessoryType = UITableView.CellAccessoryType.Checkmark
} else {
cell.accessoryType = UITableView.CellAccessoryType.None
}
}
Swift iOS
var checkedIndexPath : NSIndexPath?
// table row which row was selected
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
println("Section #\(indexPath.section)! You selected cell #\(indexPath.row)!")
if (self.checkedIndexPath != nil) {
var cell = tableView.cellForRowAtIndexPath(self.checkedIndexPath!)
cell?.accessoryType = .None
}
var cell = tableView.cellForRowAtIndexPath(indexPath)
cell?.accessoryType = .Checkmark
self.checkedIndexPath = indexPath
}// end tableView
Store the selected index path, update it in didSelectRowAt:, and from there call reloadRows(at: to reload only the old and new checked rows (not the entire table view):
var selectedIndexPath: IndexPath?
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let previous = selectedIndexPath
selectedIndexPath = indexPath
let allowDeselection = true // optional behavior
if allowDeselection && previous == selectedIndexPath {
selectedIndexPath = nil
}
tableView.reloadRows(at: [previous, selectedIndexPath].compactMap({ $0 }), with: .automatic)
tableView.deselectRow(at: indexPath, animated: true)
}
Handle the setting of the checkmark accessory view in cellForRowAt indexPath:. This will account for cell reuse and also be triggered by your above calls to reloadRows(at:, avoiding duplicating the code of setting the cell accessory:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "IDENTIFIER", for: indexPath)
cell.accessoryType = (indexPath == selectedIndexPath) ? .checkmark : .none
// ...
return cell
}

Resources