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

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

问题描述

我需要使用Xcode 6.4 / Swift 1.2以编程方式在UITableView中选择一行。

I need to select a row in a UITableView programmatically using Xcode 6.4/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)

上面给出了以下错误:

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

我的错误是什么码?
也许其他人在使用Swift 1.2时遇到过这种情况?

What is wrong with my code? Maybe someone else experienced this with Swift 1.2?

非常感谢任何帮助!

干杯

编辑:
我的UItableView已在UIViewController中的IB中创建,我试图调用上面的代码。
当我将代码放在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的子类。它还有一个连接到表格视图的
的插座,但它不叫
tableView 但是 menuTable 。然后当然你必须打电话

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)

选择该表视图的一行。

奇怪的错误消息是由
self.tableView 这一事实引起的,编译器也可以将其理解为
curried function(比较 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 1.2(Xcode 6.4)中的UITableView中选择一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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