在Swift中选择UITableViewController中的第一行

时间:2015-10-19 23:01:14

标签: swift uitableview

我试图在UITableViewController出现时(通过segue)选择第一行。

我正在使用以下代码,该代码是我之前的Objective C项目继承的:

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

        // Code that sets the custom view cell

        // Then...
        // Selecting the first row by default
        if(indexPath.row == 0)
        {
            let rowToSelect:NSIndexPath = NSIndexPath(forRow: 0, inSection: 0)

            tableView.selectRowAtIndexPath(rowToSelect, animated: true, scrollPosition: UITableViewScrollPosition.None)

            tableView.delegate?.tableView!(tableView, didSelectRowAtIndexPath: rowToSelect)
        }

        return customCell;
    }

我收到以下错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[IGANavlistTableVC tableView:didSelectRowAtIndexPath:]: unrecognized selector sent to instance 0x79e21040'

如果有人可以提供帮助,我将不胜感激。

谢谢!

编辑:

这是我的didDeselectRowAtIndexPath方法:

var navaidSelected = [String]()
var listOfNavPoints = [[String]]()

override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath)
    {
        // The selected object of the Array with all navaids data (i.e., the selected navaid) is converted to a string
        self.navaidSelected = self.listOfNavPoints[indexPath.row]
    }

1 个答案:

答案 0 :(得分:4)

您忘记了实施func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)方法。

拨打delegate时,您无需使用didSelectRowAtIndexPath

if(indexPath.row == 0)
        {
            let rowToSelect:NSIndexPath = NSIndexPath(forRow: 0, inSection: 0)

            tableView.selectRowAtIndexPath(rowToSelect, animated: true, scrollPosition: UITableViewScrollPosition.None)

           self.tableView(tableView, didSelectRowAtIndexPath: rowToSelect)
        }

=== EDITED ===

将您的didDeselectRowAtIndexPath更改为didSelectRowAtIndexPath

相关问题