[英]Two tableviews in one view controller, one table view not showing up
I have two TableView
s in one ViewController
and only one table is able to populate. 我在一个ViewController
有两个TableView
,并且只能填充一个表。 I have created outlets for both tables in my view controller. 我已经在视图控制器中为两个表创建了出口。 Here is my viewDidLoad
这是我的viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
pickerTableView.delegate = self
pickerTableView.dataSource = self
tableView.delegate = self
tableView.dataSource = self
tableView.tableFooterView = UIView()
pickerTableView.reloadData()
tableView.reloadData()
}
It seems that the only table that is read is self.tableView
and the pickerTableView
is never read in the protocols for UITableView
似乎唯一读取的表是self.tableView
,而在UITableView
的协议中从未读取pickerTableView
extension MealViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if tableView == self.tableView {
let nameHeaderView = NameHeaderView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 40))
nameHeaderView.delegate = self
nameHeaderView.sectionIndex = section
nameHeaderView.nameButton.setTitle(Data.userModels[section].name, for: .normal)
return nameHeaderView
} else {
return nil
}
}
func numberOfSections(in tableView: UITableView) -> Int {
if tableView == self.tableView {
return Data.userModels.count
} else {
return 0
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.tableView {
if Data.userModels[section].isExpandable {
return Data.userModels[section].itemModels.count
} else {
return 0
}
} else if tableView == pickerTableView {
return Data.userModels.count
}
else {
return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == self.tableView {
let cell = tableView.dequeueReusableCell(withIdentifier: "nameCells", for: indexPath) as! MealTableViewCell
cell.setup(itemModel: Data.userModels[indexPath.section].itemModels[indexPath.row])
return cell
} else if tableView == pickerTableView {
let cell2 = tableView.dequeueReusableCell(withIdentifier: "pickerCells", for: indexPath) as! NamePickerTableViewCell
cell2.setup(userModel: Data.userModels[indexPath.row])
print(Data.userModels)
return cell2
}
else {
return UITableViewCell()
}
}
} }
I have also applied constraints on each tableView
so they both appear on the screen. 我还在每个tableView
上应用了约束,因此它们都出现在屏幕上。 When I run the app, the data for self.tableView
is shown however the pickerTableView
appears empty. 当我运行该应用程序时,将显示self.tableView
的数据,但是pickerTableView
似乎为空。 self.tableView
requires sections and pickerTableView
does not require sections, could this be an issue? self.tableView
需要部分,而pickerTableView
不需要部分,这可能是一个问题吗? Also there is data in the Data.userModels
array. 在Data.userModels
数组中也有数据。
And just for reference, here is my UITableViewCell
class: 仅供参考,这是我的UITableViewCell
类:
class NamePickerTableViewCell: UITableViewCell {
@IBOutlet weak var namePickerLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func setup(userModel: UserModel) {
namePickerLabel.text = userModel.name
}
} }
Thanks for the help! 谢谢您的帮助!
If you do not need any section in pickerTableView, then also it has to have at least one section. 如果在pickerTableView中不需要任何部分,那么它也必须至少具有一个部分。
So return 1
in method numberOfSections
when tableview == self. pickerTableView
因此,当tableview == self. pickerTableView
时,在numberOfSections
方法中return 1
tableview == self. pickerTableView
tableview == self. pickerTableView
. tableview == self. pickerTableView
。
func numberOfSections(in tableView: UITableView) -> Int {
if tableView == self.tableView {
return Data.userModels.count
} else {
return 1
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.