Schemee
Schemee

Reputation: 47

Use two different custom UITableView Cells

I want to have multiple cells with each having a label in it. But as a last cell, I want to have a button instead of the label, which should navigate back to my main viewcontroller.

The labels are working, but I can't make a cell with a button in it running. Here's my Code for the UITableViewController:

class StatsViewController: UITableViewController {

   var vc = ViewController()

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        let cell2 = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath)

        if let label = cell.viewWithTag(1000)as? UILabel{
            if indexPath.row == 0 {
                label.text = "Row 0"
            }
            else if indexPath.row == 1{
               label.text = "Row 1"
            }
            else if indexPath.row == 2{
               label.text = "Row 2"
            }
            else if indexPath.row == 3{
               label.text = "Row 3"
            }
            return cell
        }
        if let button = cell2.viewWithTag(1001) as? UIButton {
                           if indexPath.row == 4{
                            return cell2
                       }
                }
        return UITableViewCell()
    }
}

I idenified the label-cell in the storyboard with "cell" and the button-cell with "cell2".

Then I tagged the label with value "1000" and the button in cell2 has Tag = 1001

Screenshot of my Storyboard

(WEITER is the Button)

Edited my code thanks to @Sh_Khan and @ like this. Now, it does show me the button BUT not the labels..

class StatsViewController: UITableViewController {

   var vc = ViewController()


    override func viewDidLoad() {
        super.viewDidLoad()
       // tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
       // tableView.reloadData()
        //tableView.delegate = self
    }

        let timestamp = DateFormatter.localizedString(from: NSDate() as Date, dateStyle: .medium, timeStyle: .short)



    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cellId = (indexPath.row == 4) ? "cell2" : "cell"

        if let label = cell.viewWithTag(1000)as? UILabel{
            if indexPath.row == 0 {
            label.text = "Row 0"
        }
        else if indexPath.row == 1{
           label.text = "Row 1"
        }
        else if indexPath.row == 2{
           label.text = "Row 2"
        }
        else if indexPath.row == 3{
           label.text = "Row 3"
        }

              return tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        }
        if let button = cell2.viewWithTag(1001) as? UIButton {
                           if indexPath.row == 4{
                            return tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
                       }
                }
           return UITableViewCell()
    }
}

Upvotes: 0

Views: 54

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100549

In your current code this

if let button = cell2.viewWithTag(1001) as? UIButton {

will never hit as the return here

return cell

will always run , You need

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
  if(indexpath.row == 19) { // last row as you have 20
    let cell2 = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath)
      return cell
   else { 
      let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    if let label = cell.viewWithTag(1000)as? UILabel{
        if indexPath.row == 0 {
            label.text = "Row 0"
        }
        else if indexPath.row == 1{
           label.text = "Row 1"
        }
        else if indexPath.row == 2{
           label.text = "Row 2"
        }
        else if indexPath.row == 3{
           label.text = "Row 3"
        }
        return cell
    }
      return cell
  }
}

Tip: instead of using tags create outlets

Upvotes: 2

Related Questions