0

我正在尝试构建一个视图,显示四个不同的视图,每个视图都tableViews不同。我想评估我在 my 中实现的函数中的不同类,但它似乎不起作用。tableViewCellstableViewtableViewCellcellForRowAtViewController

我实现UITableViewDataSource, UITableViewDelegate为超类,delegatedatasourceviewDidLoad.

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 ,它应该返回给定的单元格。

4

2 回答 2

0

老实说,你根本不需要这个变量cell。只需更改cellrealCell,然后删除这行代码:

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      
于 2019-04-19T00:21:46.673 回答
0

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

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
    }
}
于 2019-04-19T00:21:54.877 回答