Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically select a row in UITableView in Swift

I need to select a row in a UITableView programmatically using Swift 1.2.

This is the simple code:

var index = NSIndexPath(forRow: 0, inSection: 0)
self.tableView.selectRowAtIndexPath(index, animated: true, scrollPosition: UITableViewScrollPosition.Middle)
self.tableView(self.tableView, didSelectRowAtIndexPath: index)

The above gives me the following error:

Cannot invoke 'selectRowAtIndexPath' with an argument list of type '(NSIndexPath!, animated: Bool, scrollPosition: UITableViewScrollPosition)'

What is wrong with my Swift 1.2 code?

My UItableView has been created in IB in the UIViewController that I am trying to call the code above. When I put the code in a UITableViewController the compiler does not give any errors. Do I need to embed a UITableViewController in a container or is there another way?

like image 358
Mikee Avatar asked Jul 06 '15 14:07

Mikee


People also ask

Which method is used to allow moving of row in UITableView?

moveRow(at:to:) Moves the row at a specified location to a destination location.


4 Answers

Swift 3 to Swift 5 Solution

Selecting a Row

let indexPath = IndexPath(row: 0, section: 0)
myTableView.selectRow(at: indexPath, animated: true, scrollPosition: .bottom)
myTableView.delegate?.tableView!(myTableView, didSelectRowAt: indexPath)

DeSelecting a Row

let deselectIndexPath = IndexPath(row: 7, section: 0)
myTableView.deselectRow(at: deselectIndexPath, animated: true)
myTableView.delegate?.tableView!(myTableView, didDeselectRowAt: indexPath)
like image 77
Sourabh Sharma Avatar answered Oct 19 '22 15:10

Sourabh Sharma


The statement

self.tableView.selectRowAtIndexPath(index, animated: true, scrollPosition: UITableViewScrollPosition.Middle)

assumes that tableView is a property of the view controller, connected to a table view in the Storyboard. A UITableViewController, for example, already has this property.

In your case, the view controller is a not a table view controller but a subclass of a UIViewController. It also has an outlet that is connected to the table view, but it is not called tableView but menuTable. Then of course you have to call

self.menuTable.selectRowAtIndexPath(index, animated: true, scrollPosition: UITableViewScrollPosition.Middle)

to select a row of that table view.

The strange error messages are caused by the fact that self.tableView can also be understood by the compiler as a "curried function" (compare http://oleb.net/blog/2014/07/swift-instance-methods-curried-functions/).

like image 21
Martin R Avatar answered Oct 19 '22 13:10

Martin R


Use below code,after loading your table view with data:

let rowToSelect:NSIndexPath = NSIndexPath(forRow: 0, inSection: 0);  //slecting 0th row with 0th section
self.tableView.selectRowAtIndexPath(rowToSelect, animated: true, scrollPosition: UITableViewScrollPosition.None);

now,you have to manually call didSelectRowAtIndexPath: delegate method using below code:

self.tableView(self.tableView, didSelectRowAtIndexPath: rowToSelect); //Manually trigger the row to select

Thanks.

like image 20
Pankaj purohit Avatar answered Oct 19 '22 14:10

Pankaj purohit


Swift 3.x

if you want to do it at the 'cell-creation', you can do it like this

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = TableViewCell()
    let item = items[indexPath.row]

    cell.textLabel?.text    = item.title

    if (item.checked) {
        tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
    }

    return cell
}
like image 14
Sandu Avatar answered Oct 19 '22 15:10

Sandu