我面临代表问题。 下面是UITableViewController类型的DemoTable类
import Foundation
import UIKit
protocol DashboardControllerDelegate{
func companySelected()
}
class DemoTable: UITableViewController
{
var delegate : DashboardControllerDelegate?
var arr_string = [String]()
init(array: [String])
{
arr_string = array
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder)
{
fatalError("init(coder:) has not been implemented")
}
override func numberOfSections(in tableView: UITableView) -> Int
{
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return arr_string.count
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
delegate?.companySelected()
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
var cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
if cell == nil {
cell = UITableViewCell(style: .default, reuseIdentifier: "Cell")
cell?.frame(forAlignmentRect: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 30))
}
cell?.textLabel?.text = arr_string[indexPath.row]
return cell!
}
}
这是控制器类
import UIKit
class ViewController: UIViewController, DashboardControllerDelegate
{
var dataSourceTable:DemoTable?
override func viewDidLoad()
{
super.viewDidLoad()
let fruitList = UITableView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height * 0.9))
fruitList.backgroundColor = UIColor.white
let arr_Fruits = ["Mango", "Papaya", "Graps", "Apple"]
self.dataSourceTable = DemoTable(array: arr_Fruits)
fruitList.dataSource = self.dataSourceTable
fruitList.delegate = self.dataSourceTable
view.addSubview(fruitList)
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
func companySelected()
{
print("Selected")
}
}
问题是在ViewController类中未调用的委托函数“companySelected”。 帮助将是欣赏: - )
答案 0 :(得分:0)
我得到了答案。我必须添加
self.dataSourceTable?.delegate = self
在viewDidLoad()
中