Sweeper
Sweeper

Reputation: 274690

How to select a cell in table view without scrolling it?

I am building the settings screen for my app using a table VC. In the viewDidLoad, I want to show the user his/her current settings by selecting the cells. Then there will be a checkmark on the left of the selected cells. This is the didSelectrowAtIndexPath method:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(indexPath)
    for row in 0..<tableView.numberOfRowsInSection(indexPath.section) {
        let cellToBeDeselected = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: row, inSection: indexPath.section))
        cellToBeDeselected?.accessoryType = .None
    }
    cell!.accessoryType = .Checkmark
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
}

This works well. When I select a row, a checkmark appears. When I select another row, the previous checkmark disappears and a new one appears.

However, I want to select the rows programmatically in viewDidLoad, in order to show the current settings.

I found a method called selectRowAtIndexPath. I tried to use this but it requires a UITableViewScrollPosition. The docs says:

The position in the table view (top, middle, bottom) to which a given row is scrolled.

It seems that whenever I want to select a row, the table view must scroll to where the selected row is. But I just want to select it, and only select it.

How to do this?

Upvotes: 1

Views: 2036

Answers (3)

Hitendra Solanki
Hitendra Solanki

Reputation: 4941

This is purely depends on the logic.

Your are trying to deselect all the rows first, when any row is selected. As checked your current code, found that you are not maintaining the state of selected row, so it will also cause the dequeue problem i.e. when selected cell will be reused for other cell, it will show checkMark even if that is not selected.

Instead of this, you just need to maintain index variable which will store indexPath of selected row. You have 3 sections, and 3selections are allowed, in this case, you just need to maintain one array which holds values of selectedIndexPaths.

declare one property in your viewController as below.

var arraySelectedIndexPaths : Array<NSIndexPath> = Array();

In viewDidLoad: add three default value as per last selected setting.

suppose last time user have selected

row1 in section0

row2 in section1

row0 in section2

then your array will be as follow,

        //in viewDidLoad
        arraySelectedIndexPaths.append(NSIndexPath(forRow: 1, inSection: 0));
        arraySelectedIndexPaths.append(NSIndexPath(forRow: 2, inSection: 1));
        arraySelectedIndexPaths.append(NSIndexPath(forRow: 0, inSection: 2));

so in your tableView:cellForRowAtIndexPath: write below logic. this logic will show checkMark in cell if it is selected, when it comes onto the screen.

if indexPath == self.arraySelectedIndexPaths[indexPath.section] {
   cell!.accessoryType = .Checkmark
}else{
   cell!.accessoryType = .None
}

And in tableView:didSelectedRowAtIndexPath: write logic as below. This will update indexPath in array as per selection. Will also deSelect previous row and select new row.

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(indexPath)

//deSelected previously selected Cell
        let cellToBeDeselected = tableView.cellForRowAtIndexPath(self.arraySelectedIndexPaths[indexPath.section])
        cellToBeDeselected?.accessoryType = .None

    cell!.accessoryType = .Checkmark
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
    self.arraySelectedIndexPaths[indexPath.section] = indexPath;//update new selected indexPath in array
}

I have wrote above logic, as per our discussion in comments for your question. below is our discussion.

@Sweeper are you trying to show only check mark in one row, based on the last setting value ? – HitendraHckr

@HitendraHckr Yes I am. I am trying to show check mark in three different rows, since there are three settings – Sweeper

so, do you have 3 sections for different 3 settings ? – Hitendra Hckr

and I think any can be selected in one section, am I right ? – Hitendra Hckr

@HitendraHckr Yes you're right – Sweeper

Upvotes: 1

Crazy Developer
Crazy Developer

Reputation: 3464

you can directly call did select method of table view using the below code

let indexpath = NSIndexPath(forRow: yourRow, inSection: yourColumn);
self.tableView(tblObj, didSelectRowAtIndexPath: indexpath);

Actually you want to call did select to perform some operation which can be manage by this. Try this, it may be work for you.

Upvotes: 1

user4151918
user4151918

Reputation:

As the documentation mentions, you can specify UITableViewScrollPositionNone for the scroll position, which will result in no scrolling.

Upvotes: 5

Related Questions