Swift3 发出「;DidSelectRowatIndeXPath“;斯威夫特3
我对斯威夫特还很陌生,但大部分时间我都能跟上。然而,我的代码有一个问题,代码本身甚至无法识别(我没有收到任何错误迹象)。我试图单击表视图中的一行,使其进入下一页,但我认为代码无法识别didselectrow方法。也许你们当中更有经验的人可以帮我 代码如下:Swift3 发出「;DidSelectRowatIndeXPath“;斯威夫特3,swift3,didselectrowatindexpath,Swift3,Didselectrowatindexpath,我对斯威夫特还很陌生,但大部分时间我都能跟上。然而,我的代码有一个问题,代码本身甚至无法识别(我没有收到任何错误迹象)。我试图单击表视图中的一行,使其进入下一页,但我认为代码无法识别didselectrow方法。也许你们当中更有经验的人可以帮我 代码如下: import UIKit import Foundation class OnePercentersViewController: UIViewController, UITableViewDataSource { var cop
import UIKit
import Foundation
class OnePercentersViewController: UIViewController, UITableViewDataSource
{
var copiedExecutiveArray : [String] = NSArray() as! [String]
var identities : [String] = NSArray() as! [String]
override func viewDidLoad()
{
super.viewDidLoad()
let copyExecutiveNames : ExecutiveArray = ExecutiveArray()
copiedExecutiveArray = copyExecutiveNames.executiveNames
identities = copyExecutiveNames.executiveNames
}
//how many sections in table
func numberOfSections(in tableView: UITableView) -> Int
{
return 1
}
//returns int (how many rows)
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return copiedExecutiveArray.count
}
//contents of each cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")//UITableViewCell()
let personName = copiedExecutiveArray[indexPath.row]
cell?.textLabel?.text = personName
return cell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
let execName = identities[indexPath.row]
let viewController = storyboard?.instantiateViewController(withIdentifier: execName)
self.navigationController?.pushViewController(viewController!, animated: true)
print("button clicked")
tableView.deselectRow(at: indexPath as IndexPath, animated: true)
}
}
我没有检查代码的其他部分,但要使
tableView(\uquot:didSelectRowAt:)
工作,您的ViewController需要符合UITableViewDelegate
:
class OnePercentersViewController: UIViewController, UITableViewDataSource, UITableViewDelegate
您还需要将UITableView
的委托
连接到故事板中的ViewController。或者,如果您有一个@IBOutlet
到UITableView
,您也可以在代码中完成。无论如何,不仅数据源
,而且委托
这不是一个关键问题,但您可以将类的前两行写为:
var copiedExecutiveArray : [String] = []
var identities : [String] = []
在花了一天半的时间观看Youtube视频和阅读类似问题的评论后,tt进行了反复尝试,但多亏了你们,我终于成功了!当我读到我必须通过故事板将代理连接到ViewController时,它终于点击了,谢谢 以下是类似问题的最终代码:
import UIKit
import Foundation
class OnePercentersViewController: UIViewController,UITableViewDataSource, UITableViewDelegate
{
var copiedExecutiveArray : [String] = []
var identities : [String] = []
override func viewDidLoad()
{
super.viewDidLoad()
let copyExecutiveNames : ExecutiveArray = ExecutiveArray()
copiedExecutiveArray = copyExecutiveNames.executiveNames
identities = copyExecutiveNames.executiveNames
}
//how many sections in table
func numberOfSections(in tableView: UITableView) -> Int
{
return 1
}
//returns int (how many rows)
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return copiedExecutiveArray.count
}
//contents of each cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let personName = copiedExecutiveArray[indexPath.row]
cell.textLabel?.text = personName
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
let execName = identities[indexPath.row]
let viewController = storyboard?.instantiateViewController(withIdentifier: execName)
self.navigationController?.pushViewController(viewController!, animated: true)
tableView.deselectRow(at: indexPath as IndexPath, animated: true)
}
}
不相关,但
var标识:[String]=NSArray()as![字符串]
太可怕了。只需编写var identies=[String]()
谢谢,我会尝试一下。如何通过故事板连接代理(