Set Checkmark in Uitableview

UITableViewCell checkmark change on select

  1. Keep a property in your view controller called selectedRow, which represents the index of a row that represents the checked item in a table section.

  2. In your view controller's -tableView:cellForRowAtIndexPath: delegate method, set the accessoryType of the cell to UITableViewCellAccessoryCheckmark if the cell's indexPath.row equals the selectedRow value. Otherwise, set it to UITableViewCellAccessoryNone.

  3. In your view controller's -tableView:didSelectRowAtIndexPath: delegate method, set the selectedRow value to the indexPath.row that is selected, e.g.: self.selectedRow = indexPath.row

How to add a checkmark to cells in UITableView (using a custom UITableViewCell subclass and dequeueReusableCell)

You should set your accessoryType in the cellForRowAt method, try to rewrite it as this:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{
let cell = tableView.dequeueReusableCell(withIdentifier: "TableCell", for: indexPath) as! TableCell

cell.accessoryType = .checkmark

guard let arrayOneIndex = currentArrayOneIndex, arrayOneIndex < arrayOne.count else { return cell }

let currentArrayOne = arrayOne[arrayOneIndex]

guard let tableCells = currentArrayOne.tableCells, indexPath.row < tableCells.count else { return cell }

cell.tableLabel.text = tableCells[indexPath.row]

return cell
}

UITableViewCell checkmark to be toggled on and off when tapped

Swift > 3.0

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) {
cell.accessoryType = .none
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) {
cell.accessoryType = .checkmark

}
}

I solved by using two Swift functions: the didSelectRowAtIndexPath and the didDeselectRowAtIndexPath.

override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
cell.accessoryType = .None
}
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
cell.accessoryType = .Checkmark

}
}

To make this work properly, add a line of code to your cellForRowAtIndexPath function to select a row when the table view is drawn on the screen, otherwise the didDeselectRowAtIndexPath will not be called the first time you select another row. Like so:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cellData", forIndexPath: indexPath)
if (some condition to initially checkmark a row)
cell.accessoryType = .Checkmark
tableView.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: UITableViewScrollPosition.Bottom)
} else {
cell.accessoryType = .None
}

return cell
}

Swift UITableview cell checkmark with selection blink option

For reusability, you need a variable to save the selected cell index(or array for cells) and then set the appropriate accessoryType when displaying the cell.

For single selection, remember to clear the previous selection.

A sample pattern for single selection:

var selectedRow = 0

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedRow = indexPath.row
for cell in tableView.visibleCells { //Why not using didDeselectRowAt? Because the default selected row(like row 0)'s checkmark will NOT be removed when clicking another row at very beginning.
cell.accessoryType = .none
}
tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
}

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.accessoryType = indexPath.row == selectedRow ? .checkmark : .none
}

A pattern for multiple selections (Updated for multi-sections):

var selectedIndexPaths = Set<IndexPath>()

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if selectedIndexPaths.contains(indexPath) { //deselect
selectedIndexPaths.remove(indexPath)
tableView.cellForRow(at: indexPath)?.accessoryType = .none
}
else{
selectedIndexPaths.insert(indexPath) //select
tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
}
}

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.accessoryType = selectedIndexPaths.contains(indexPath) ? .checkmark : .none
}

how to add checkmark in tableview swift

Just do following changes in your code to maintain checkmark into tableview while you are scrolling tableview

Sample Image

Result :

Sample Image

Its work fine now, Any problem let me know i will definitely help you to out.Enjoy..

Selecting TableView Cell Activates Checkmark in Rows in Multiple Sections

use data store for save checkmarks like this:

var selectedIngredients: Set<IndexPath> = [] // use set for unique save

then didSelect callBack:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
if self.selectedIngredients.contains(indexPath) {
self.selectedIngredients.remove(indexPath)

} else {
self.selectedIngredients.insert(indexPath)
}

self.tableView.reloadData()
}

after reload in CellForRow:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if selectedIngredients.contains(indexPath) {
cell.accessoryType = .checkmark
} else {
cell.accessoryType = .none
}
}

If you want it to have only one Row contain checkmark:

var selectedIngredients: IndexPath? = nil

and didSelect CallBack:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
self.selectedIngredients = indexPath
}

and finally:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if selectedIngredients == indexPath {
cell.accessoryType = .checkmark
} else {
cell.accessoryType = .none
}
}

Set one check mark for each section in UITableView and save its state

I have found a precise and perfect solution,thanks to Lithu T.V and Nithin Gobel for guiding me in the right direction.Unfortunately their solutions didn't help me in achieving 1 check mark per section,but multiple check marks in fact.So I thought of saving the selected row in user defaults and for initially selecting 1st row of each section,we declare first and second section index,assign to index path and then assign cell accessory view as check mark.Here we go,let's first deal with cellForRowAtIndexPath:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.row,indexPath.section];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor whiteColor];
NSUserDefaults *savedState = [NSUserDefaults standardUserDefaults];

switch (indexPath.section)
{
case 0:
{
NSNumber *indexNumber = [NSNumber numberWithInteger:indexPath.row];
if([[savedState objectForKey:@"firstSectionIndex"] isEqual: indexNumber])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.firstSectionIndex = indexPath;
}

if (indexPath.row == 0)
{
NSObject * checkedCellObject = [savedState objectForKey:@"firstSectionIndex"];
if(checkedCellObject == nil)
{
self.firstSectionIndex = indexPath;
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
cell.textLabel.text = @"Yes";
}
if (indexPath.row == 1)
{
cell.textLabel.text = @"No";
}
}
break;
case 1:
{
NSNumber *indexNumber = [NSNumber numberWithInteger:indexPath.row];
if([[savedState objectForKey:@"secondSectionIndex"] isEqual: indexNumber])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
secondSectionIndex = indexPath;
}

if (indexPath.row == 0)
{
NSObject * checkedCellObject = [savedState objectForKey:@"secondSectionIndex"];
if(checkedCellObject == nil)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
secondSectionIndex = indexPath;
}
cell.textLabel.text = @"Easy";
}
if (indexPath.row == 1)
{
cell.textLabel.text = @"Medium";
}
if (indexPath.row == 2)
{
cell.textLabel.text = @"Hard";
}
}
break;

default:
break;
}
}

tableView.backgroundColor = [UIColor clearColor];
tableView.backgroundView = nil;

return cell;
}

Please observe in each case,we are checking if the checked state is in saved user defaults,if it is nil,we check 1st cell(row) of each section,here we go with table view delegate method,did select row at index path:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];

if (indexPath.section == 0)
{
UITableViewCell *checkCell = [tableView cellForRowAtIndexPath:indexPath];
if(firstSectionIndex && firstSectionIndex != indexPath)
{
UITableViewCell *uncheckCell = [tableView cellForRowAtIndexPath:firstSectionIndex];
uncheckCell.accessoryType = UITableViewCellAccessoryNone;
}
self.firstSectionIndex = indexPath;
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:firstSectionIndex.row] forKey:@"firstSectionIndex"];
checkCell.accessoryType = UITableViewCellAccessoryCheckmark;
}

if (indexPath.section == 1)
{
UITableViewCell *checkCell = [tableView cellForRowAtIndexPath:indexPath];
if(secondSectionIndex)
{
UITableViewCell *unchekCell = [tableView cellForRowAtIndexPath:secondSectionIndex];
unchekCell.accessoryType = UITableViewCellAccessoryNone;
}
self.secondSectionIndex = indexPath;
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:secondSectionIndex.row] forKey:@"secondSectionIndex"];
checkCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}

That's it,we have successfully accomplished one check mark per section and also save the accessory state of cell in user defaults for long term reference.

Hope this helps some one,thanks :)

UITableView Checkmark not appearing when selected

Solved!

My Mistake was i needed to add cell.setSelectionStype = .default in cellForAtRow

UITableViewCellAccessoryType Change checkmark colour

The following code should work:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell

//Change cell's tint color
cell.tintColor = UIColor.redColor()

//Set UITableViewCellAccessoryType.Checkmark here if necessary
cell.accessoryType = .Checkmark

/* ... */

return cell
}


Related Topics



Leave a reply



Submit