我在UITableView
内创建了两个UIViewController
。要链接到delegate
和datasource
,我使用了基本方法,按住Ctrl并拖动到上方的“黄色球”。但是,当每个UITableView
应该分别在类中具有自己的数据库时,我只能用一个来做到这一点。当我对第二个UITableView
执行相同操作时,显然它会将相同的datasource
和delegate
链接到两者,而没有从类中提取数据到第二个并将它们显示在屏幕上。
我该如何解决?
//I declared four buttons, two in each table view.
@IBOutlet weak var btnDrop: UIButton!
@IBOutlet weak var tblView: UITableView!
@IBOutlet weak var btnDropProj: UIButton!
@IBOutlet weak var tblViewProj: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tblView.isHidden = true
tblViewProj.isHidden = true
}
var selectClient = ["Cliente 1", "Cliente 2", "Cliente 3", "Cliente 4", "Cliente 5", "Cliente 6", "Cliente 7", "Cliente 8"]
var selectProject = ["Projeto 1", "Projeto 2", "Projeto 3", "Projeto 4", "Projeto 5", "Projeto 6", "Projeto 7", "Projeto 8"]
答案 0 :(得分:0)
在我看来,您正在尝试将表视图链接到2个不同的数据源。
您可以先创建两个单独的数据源,如下所示。
class Table1DataSource: NSObject, UITableViewDataSource {
// Properties
private var table1Data: [String]
init(table1Data: [String]) {
self.table1Data = table1Data
super.init()
}
// MARK: - Data Source
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.table1Data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var profileCell: ProfileCell
let cell = tableView.dequeueReusableCell(withIdentifier: Table1Cell.reuseIdentifier, for: indexPath) as! Table1Cell
// Initialize the cell here
return cell
}
}
之后,您可以将数据源链接到控制器中的表视图。
class MainController: UIViewController {
// Outlets
@IBOutlet weak var tblView: UITableView!
@IBOutlet weak var tblViewProj: UITableView!
// Properties
var selectClient = ["Cliente 1", "Cliente 2", "Cliente 3", "Cliente 4", "Cliente 5", "Cliente 6", "Cliente 7", "Cliente 8"]
var selectProject = ["Projeto 1", "Projeto 2", "Projeto 3", "Projeto 4", "Projeto 5", "Projeto 6", "Projeto 7", "Projeto 8"]
// DataSource
lazy var tblViewDataSource: Table1DataSource = {
return Table1DataSource(table1Data: self.selectClient)
}()
override func viewDidLoad() {
super.viewDidLoad()
self.tblView.dataSource = self.tblViewDataSource
}
}
剩下要做的就是重复链接第二张表的步骤。
希望有帮助!
答案 1 :(得分:0)
// MARK: - UITableView datasource
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.tblView {
return selectClient.count
}
else {
return selectProject.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
if tableView == self.tblView {
let CellIdentifier: String = "YOURCUSTOMCELL"
var cell: YOURCUSTOMCELL? = (tableView.dequeueReusableCell(withIdentifier: CellIdentifier) as? YOURCUSTOMCELL)
if cell == nil {
let topLevelObjects: [Any] = Bundle.main.loadNibNamed("YOURCUSTOMCELL", owner: nil, options: nil)!
cell = (topLevelObjects[0] as? YOURCUSTOMCELL)
cell?.selectionStyle = .none
}
cell?.backgroundColor = UIColor.white
return cell!
}
else {
let CellIdentifier: String = "YOURCUSTOMCELL"
var cell: YOURCUSTOMCELL? = (tableView.dequeueReusableCell(withIdentifier: CellIdentifier) as? YOURCUSTOMCELL)
if cell == nil {
let topLevelObjects: [Any] = Bundle.main.loadNibNamed("YOURCUSTOMCELL", owner: nil, options: nil)!
cell = (topLevelObjects[0] as? YOURCUSTOMCELL)
cell?.selectionStyle = .none
}
cell?.backgroundColor = UIColor.white
return cell!
}
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
if tableView == self.tblView {
return 50.0
}
else {
return 50.0
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView == self.tblView {
}
else {
}
}