I am trying to call didSelectRowAtIndexPath programmatically but am having trouble.
[self tableView:playListTbl didSelectRowAtIndexPath:indexPath];
Starting from Mistalis answer, I made this little ExtendedCell
class because I use it in several table views.
Usage in table view:
// Here MyTableView is UITableView and UITableViewDelegate
// but you can separate them.
class MyTableView: UITableView, UITableViewDelegate {
override func dequeueReusableCell(withIdentifier identifier: String) -> UITableViewCell? {
return MyCell(identifier: identifier, table: self)
}
// ...
}
Implementation of your cell:
class MyCell : ExtendedCell {
convenience init(identifier: String?, table: MyTableView)
{
// in this example, MyTableView is both table and delegate
self.init(identifier: identifier, table: table, delegate: table)
// ...
}
// At any moment you may call selectTableRow() to select
// the row associated to this cell.
func viewItemDetail() {
selectTableRow()
}
}
ExtendedCell
class:
import UIKit
/**
* UITableViewCell with extended functionality like
* the ability to trigger a selected row on the table
*/
class ExtendedCell : UITableViewCell {
// We store the table and delegate to trigger the selected cell
// See: https://stackoverflow.com/q/18245441/manually-call-did-select-row-at-index-path
weak var table : UITableView!
weak var delegate : UITableViewDelegate!
convenience init(identifier: String?, table: UITableView, delegate: UITableViewDelegate)
{
self.init(style: .default, reuseIdentifier: identifier)
self.table = table
self.delegate = delegate
}
func selectTableRow()
{
if let indexPath = table.indexPath(for: self) {
table.selectRow(at: indexPath, animated: true, scrollPosition: .none)
delegate.tableView!(table, didSelectRowAt: indexPath)
}
}
}