本文介绍了在 Swift 中选择 UITableViewController 中的第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在 UITableViewController 出现后选择第一行(通过 segue).
I am trying to select the first row in the UITableViewController once it appears (through a segue).
我正在使用从我之前的 Objective C 项目中继承的以下代码:
I am using the following code which I inherited from my previous Objective C project:
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'
如果有人能提供帮助,我将不胜感激.
I would appreciate if someone could help.
谢谢!
这是我的 didDeselectRowAtIndexPath 方法:
This is my didDeselectRowAtIndexPath method:
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]
}
推荐答案
你忘记实现 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
方法.
调用didSelectRowAtIndexPath
时不需要使用delegate
:
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)
}
=== 编辑 ===
将您的 didDeselectRowAtIndexPath
更改为 didSelectRowAtIndexPath
.
这篇关于在 Swift 中选择 UITableViewController 中的第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!