【问题标题】:How do I set up a view with multiple Table Views and different TableViewCells for each Table View?如何为每个 Table View 设置具有多个 Table View 和不同 TableViewCells 的视图?
【发布时间】:2019-09-09 07:52:10
【问题描述】:

我正在尝试构建一个视图,该视图显示四个不同的tableViews,每个tableView 具有不同的tableViewCells。我想评估我在ViewController 中实现的cellForRowAt 函数中的不同tableViewCell 类,但它似乎不起作用。

我将UITableViewDataSource, UITableViewDelegate 实现为超类,并为viewDidLoad 中的每个表视图编写了delegatedatasource

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{   

    //Outlets

    @IBOutlet weak var scheduleView: UIView!
    @IBOutlet weak var scheduleTableView: UITableView!

    @IBOutlet weak var goalsView: UIView!
    @IBOutlet weak var goalsTableView: UITableView!

    @IBOutlet weak var toDoView: UIView!
    @IBOutlet weak var toDoTableView: UITableView!

    @IBOutlet weak var motivationView: UIView!
    @IBOutlet weak var motivationTableView: UITableView!


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        scheduleView.clipsToBounds = true
        scheduleView.layer.cornerRadius = 15
        goalsView.clipsToBounds = true
        goalsView.layer.cornerRadius = 15
        toDoView.clipsToBounds = true
        toDoView.layer.cornerRadius = 15
        motivationView.clipsToBounds = true
        motivationView.layer.cornerRadius = 15

        // Delegate & Datasources
        scheduleTableView.delegate = self
        scheduleTableView.dataSource = self
        goalsTableView.delegate = self
        goalsTableView.dataSource = self
        toDoTableView.delegate = self
        toDoTableView.dataSource = self
        motivationTableView.delegate = self
        motivationTableView.dataSource = self

    }
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        var realCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "scheduleCell", for: indexPath)


        if tableView == scheduleTableView{
            var cell = tableView.dequeueReusableCell(withIdentifier: "scheduleCell", for: indexPath)
            cell.textLabel?.text = "Text"

        }else if tableView == goalsTableView{
            var cell = tableView.dequeueReusableCell(withIdentifier: "goalsCell", for: indexPath)
            cell.textLabel?.text = "Text"

        }else if tableView == toDoTableView{
            var cell = tableView.dequeueReusableCell(withIdentifier: "toDoCell", for: indexPath)
            cell.textLabel?.text = "Text"


        }else if tableView == motivationTableView{
            var cell = tableView.dequeueReusableCell(withIdentifier: "motivationCell", for: indexPath) as! motivationTableViewCell
            cell.textLabel?.text = "Text"
            cell.motivationImage.image = #imageLiteral(resourceName: "placeholderImage")
            cell.motivationLabel.text = "TestText"

            realCell = cell
        }
        return realCell      
    }

如果我运行此代码,realCell 变量将返回 nil 相反,它应该返回给定的单元格。

【问题讨论】:

    标签: swift uitableview delegates tableview datasource


    【解决方案1】:

    不必为使单元格出列的初始调用而烦恼。

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if tableView == scheduleTableView {
            var cell = tableView.dequeueReusableCell(withIdentifier: "scheduleCell", for: indexPath)
            cell.textLabel?.text = "Text"
    
            return cell
        } else if tableView == goalsTableView {
            var cell = tableView.dequeueReusableCell(withIdentifier: "goalsCell", for: indexPath)
            cell.textLabel?.text = "Text"
    
            return cell
        } else if tableView == toDoTableView {
            var cell = tableView.dequeueReusableCell(withIdentifier: "toDoCell", for: indexPath)
            cell.textLabel?.text = "Text"
    
            return cell
        } else /*if tableView == motivationTableView*/ {
            var cell = tableView.dequeueReusableCell(withIdentifier: "motivationCell", for: indexPath) as! motivationTableViewCell
            cell.textLabel?.text = "Text"
            cell.motivationImage.image = #imageLiteral(resourceName: "placeholderImage")
            cell.motivationLabel.text = "TestText"
    
            return cell
        }
    }
    

    【讨论】:

      【解决方案2】:

      老实说,您根本不需要变量cell。只需将cell 更改为realCell,并删除这行代码:

      realCell = cell
      

      最终代码:

      var realCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "scheduleCell", for: indexPath)
      
      
      if tableView == scheduleTableView {
          realCell = tableView.dequeueReusableCell(withIdentifier: "scheduleCell", for: indexPath)
          realCell.textLabel?.text = "Text"
      } else if tableView == goalsTableView {
          realCell = tableView.dequeueReusableCell(withIdentifier: "goalsCell", for: indexPath)
          realCell.textLabel?.text = "Text"
      } else if tableView == toDoTableView {
          realCell = tableView.dequeueReusableCell(withIdentifier: "toDoCell", for: indexPath)
          realCell.textLabel?.text = "Text"
      } else if tableView == motivationTableView {
          realCell = tableView.dequeueReusableCell(withIdentifier: "motivationCell", for: indexPath) as! motivationTableViewCell
          realCell.textLabel?.text = "Text"
          realCell.motivationImage.image = #imageLiteral(resourceName: "placeholderImage")
          realCell.motivationLabel.text = "TestText"
      }
      
      return realCell      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-26
        • 2020-01-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多