在我的扩展代码中调用数组时使用未解析的标识符错误



我正在尝试创建一个相当简单的下拉菜单,我已经为其创建了一个包含一些要显示的字符串的数组。我不确定我哪里出错了,但是当我编写扩展代码以将数组添加到要在下拉列表中显示的 UITableView 时,数组"rList"无法识别。在调用"rList"的扩展代码中显示未解决的标识符错误。另请注意,我正在运行Xcode 9测试版,不确定某些语法是否有我可能错过的更改。感谢我能得到的任何帮助!

干杯

(这是代码(

import UIKit

class MainViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var dropDownButton: UIButton!
var rList = ["A", "B", "C", "D", "E", "F"]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.isHidden = true
// Do any additional setup after loading the view.
}

@IBAction func dropDownButtonPressed(_ sender: Any) {
if tableView.isHidden {
animate(toggle: true)
} else {
animate(toggle: false)
}
}

func  animate(toggle:Bool) {
if toggle {
UIView.animate(withDuration: 0.3) {
self.tableView.isHidden = false
}
} else {
UIView.animate(withDuration: 0.3) {
self.tableView.isHidden = true
}
}
}
}

现在为我的扩展代码

extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return rList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = rList[indexPath.row]
return cell
}
}

您正在扩展ViewController(这是应用程序模板提供的默认空视图控制器(而不是MainViewControllerViewController在其模板形式中没有rList作为成员,因此错误。

将扩展更改为:

extension MainViewController: UITableViewDelegate, UITableViewDataSource {
.
.
.
}

最新更新