一个视图中有 2 table 个视图 ios

2 table views in one view ios

我有 2 个表

如何将两个自定义单元格调用到 cellForRowAt...

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "incomeCell", for: indexPath) as! ViewControllerTableViewCell

        return cell
    }

如何在同一个函数中添加 expensesCell 以便我的两个表都填充数据?

对不同表格视图的元素使用数组,这样您将拥有:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if tableView == incomeTable {
        let cell = tableView.dequeueReusableCell(withIdentifier: "incomeCell", for: indexPath) as! ViewControllerTableViewCell

        return cell
    } else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "expenseCell", for: indexPath) as! ViewControllerTableViewCell

        return cell
    }
}

并记住:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if tableView == incomeTable {
        return incomeElements.count
    } else {
        return expensesElements.count
    }
}

一个可能的解决方案是为 table 个视图之一添加一个插座

@IBOutlet weak var incomeTableView : UITableView!

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if tableView == incomeTableView {
        let cell = tableView.dequeueReusableCell(withIdentifier: "incomeCell", for: indexPath) as! ViewControllerTableViewCell
        return cell
    } else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "expensesCell", for: indexPath) as! ViewControllerTableViewCell
        return cell
    }
}

但是您可以使用 one table 使用部分的视图实现相同的外观。