简体   繁体   English

一个视图控制器中有两个表视图,一个表视图未显示

[英]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.

相关问题 一个View Controller中有两个tableViews问题? - Two tableViews in one View Controller issue? 在一个视图控制器中将数据源分配给两个表视图 - assigning datasource to two tableviews in one view controller 一个视图中的两个表视图 controller 表视图的高度应等于其内容 - Two tableviews in one view controller and height of tableviews should be equal to its content 一个控制器中的两个表视图 - Two Tableviews in one Controller 一个视图中的几个表视图 - several tableviews in one view 将DS设置为第二个视图时,一个视图控制器中的两个表视图和一个表视图不起作用 - Two Tableviews in One view controller and One tableview not working when setting DS for 2nd one 仅实例化一个时,两个UISearchBar出现在视图控制器中 - Two UISearchBars showing up in a view controller when only one is instantiated 在一个视图控制器中使用swift 4.0中的segment获得两个tableview,而无需使用XIB CELL - get two tableviews in one view controller using segment in swift 4.0 without using XIB CELL 如何在一个视图控制器中为表视图分配标签值 - How to assign tag values for tableviews in one view controller 试图在一个视图控制器中获得两个UICollection视图。 只有一个出现 - Trying to get two UICollection Views in one view controller. only one showing up
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM