Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell Set selected initially

Hi there I have situation where in an iPad app my master controller has list and details controller have its details , a typical UISplitViewController pattern. What I want to achieve is , my first row should be initially selected and later I want to give selection choice to user. I am using cell's setSelected and in didSelectRowAtIndexPath method removing selection like this.

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    NSIndexPath* path = [NSIndexPath indexPathForRow:0 inSection:0];
    UITableViewCell* cell = [tableView cellForRowAtIndexPath:path];
    [cell setSelected:NO];
} 

for initial cell but my none cell is getting selected after that please help me out.

like image 720
Sagar In Avatar asked Oct 10 '13 12:10

Sagar In


9 Answers

For the cell to appear selected, you have to call -setSelected:animated: from within -tableView:willDisplayCell:forRowAtIndexPath: like so:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (/* should be selected */) {
        [cell setSelected:YES animated:NO];
    }
}

Calling -setSelected from anywhere else has no effect.

like image 85
Drew C Avatar answered Oct 06 '22 00:10

Drew C


try this

NSIndexPath* selectedCellIndexPath= [NSIndexPath indexPathForRow:0 inSection:0];
[self tableView:tableViewList didSelectRowAtIndexPath:selectedCellIndexPath];
[tableViewList selectRowAtIndexPath:selectedCellIndexPath animated:YES scrollPosition:UITableViewScrollPositionNone];

OR

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (/* should be selected */) {
        [cell setSelected:YES animated:NO];
    }
}

Swift 4 version is

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    func shouldSelect() -> Bool {
        // Some checks
    }
    if shouldSelect() {
            tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
    }
}
like image 30
Ravindhiran Avatar answered Oct 05 '22 23:10

Ravindhiran


I don't know you are expecting this answer. By default if you wants to select first row in your tableView with out manual selection, just initiate the didSelectRowAtIndexPath method like this

- (void)viewDidAppear:(BOOL)animated {
    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
    [myTableView selectRowAtIndexPath:indexPath animated:YES  scrollPosition:UITableViewScrollPositionBottom];
}
like image 36
wesley Avatar answered Oct 06 '22 00:10

wesley


Best Option

Swift 5

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if shouldSelectThisRow {
        tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
    } else {
        tableView.deselectRow(at: indexPath, animated: false)
    }
}
like image 26
Lal Krishna Avatar answered Oct 06 '22 01:10

Lal Krishna


Swift 4: Selecting cell initially

import UIKit

class MyViewController: UIViewController, UITableViewDelegate {
    @IBOutlet weak var tableView: UITableView!
    ...

     func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

        if /* your code here */ == indexPath.row {
            tableView.selectRow(at: indexPath, animated: false, scrollPosition: UITableView.ScrollPosition.none)
        }
    }

    ...
}
like image 25
Peter Kreinz Avatar answered Oct 06 '22 01:10

Peter Kreinz


It will also help in the case of POP Navigation

.h file,

NSIndexPath *defaultSelectedCell

.m File

Put this code in ViewDidLoad

defaultSelectedCell= [NSIndexPath indexPathForRow:0 inSection:0];

in viewDidAppear

[self.tableView selectRowAtIndexPath:defaultSelectedCell animated:NO  scrollPosition:UITableViewScrollPositionNone];

This will help when you POP, Put this code in viewWillAppear

[self.catTableView selectRowAtIndexPath:defaultSelectedCell animated:NO scrollPosition:UITableViewScrollPositionNone];

in tableView didSelectRowAtIndexPath Method,

defaultSelectedCell = [NSIndexPath indexPathForRow:indexPath.row inSection:0];

This helps me perfectly either first time loading or Pop Navigation.

like image 40
Ashu Avatar answered Oct 05 '22 23:10

Ashu


Swift 4 and 5: Selecting cells initially just one time without willDisplayCell resetting their status:

override func viewWillAppear(_ animated: Bool) {
    for section in 0..<tableView.numberOfSections {
        for row in 0..<tableView.numberOfRows(inSection: section) {
            let indexPath = IndexPath(row: row, section: section)

            if /* your condition for selecting here */ {
                _ = tableView.delegate?.tableView?(tableView, willSelectRowAt: indexPath) // remove if you don't want to inform the delegate
                tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
                tableView.delegate?.tableView?(tableView, didSelectRowAt: indexPath) // remove if you don't want to inform the delegate
            }
        }
    }
}
like image 21
Jonathan Cabrera Avatar answered Oct 06 '22 01:10

Jonathan Cabrera


Swift 5, Thanks @Ravindhiran's answer a lot:

let selectedCellIndexPath = IndexPath(row: 0, section: 0)
tableView(tableViewList, didSelectRowAt: selectedCellIndexPath)
tableViewList.selectRow(at: selectedCellIndexPath, animated: false, scrollPosition: .none)

or

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if /* should be selected */{
          cell.setSelected(true, animated: false)
     }
}
like image 43
dengST30 Avatar answered Oct 05 '22 23:10

dengST30


Swift 5.2

override func setSelected(_ selected: Bool, animated: Bool) {

        super.setSelected(selected, animated: true)
        accessoryType = selected ? .checkmark : .none
        backgroundColor = selected ? UIColor.secondarySystemBackground : UIColor.systemBackground
        myLabel.textColor = selected ? .tertiaryLabel : .secondaryLabel
        self.isUserInteractionEnabled = selected ? false : true

    }

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

        let cell = cell as! CustomTableViewCell

        if (/* condition is met */) {
            cell.setSelected(true, animated: true)

        }
    }

like image 42
Tech UK Avatar answered Oct 05 '22 23:10

Tech UK