如何在Swift中的UITableView中以编程方式选择一行 [英] How to programmatically select a row in UITableView in Swift

查看:73
本文介绍了如何在Swift中的UITableView中以编程方式选择一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用Swift 1.2以编程方式在UITableView中选择一行.

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

这是简单的代码:

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:

无法使用类型为(NSIndexPath !,动画:布尔,scrollPosition:UITableViewScrollPosition)"的参数列表调用"selectRowAtIndexPath"

我的Swift 1.2代码有什么问题?

What is wrong with my Swift 1.2 code?

我的UItableView已在IB中的UIViewController中创建,我正在尝试调用上面的代码. 当我将代码放入UITableViewController时,编译器不会给出任何错误. 我需要在容器中嵌入UITableViewController还是有其他方法?

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?

推荐答案

语句

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

假定tableView是视图控制器的属性,已连接 到情节提要中的表格视图.例如,UITableViewController已经具有 财产.

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.

在您的情况下,视图控制器不是表视图控制器 但是UIViewController的子类.它也有一个出口是 连接到表视图,但未调用 tableViewmenuTable.那当然要打电话了

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.

奇怪的错误消息是由以下事实引起的: self.tableView也可以被编译器理解为 咖喱函数"(比较 http://oleb.net/blog/2014/07 /swift-instance-methods-curried-functions/).

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/).

这篇关于如何在Swift中的UITableView中以编程方式选择一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆