Как мне настроить представление с несколькими табличными представлениями и различными TableViewCell для каждого табличного представления? - PullRequest
0 голосов
/ 19 апреля 2019

Я пытаюсь создать вид, который показывает четыре разных tableViews с разными tableViewCells для каждого tableView.Я хотел бы оценить различные tableViewCell классы в функции cellForRowAt, которую я реализовал в моем ViewController, но, похоже, она просто не работает.

Я реализовал UITableViewDataSource, UITableViewDelegate как суперклассыи записал delegate и datasource для каждого табличного представления в viewDidLoad.

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, она должна возвращать данные ячейки.

Ответы [ 2 ]

0 голосов
/ 19 апреля 2019

Не беспокойтесь об этом первоначальном вызове, чтобы исключить ячейку из очереди.

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
    }
}
0 голосов
/ 19 апреля 2019

Честно говоря, вам не нужна переменная 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      
...