Tom
Tom

Reputation: 197

Add bottom cell to dynamic table view

I have a table view with a dynamic cell:

extension CarViewController: UITableViewDataSource, UITableViewDelegate {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return carsArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let rowData = carsArray[indexPath.row]

        let cell = tableView.dequeueReusableCell(withIdentifier: "carCell") as! CarCell
        cell.setButton(name: rowData.name)

        return cell
    }

    func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
        for n in 0...carsArray.count - 1 {
            if indexPath.row == n {

                performSegue(withIdentifier: "goToEditCar", sender: self)

            }
        }

        return indexPath
    }
}

It works fine, but how can I add a further cell at the bottom of the table view with custom content?

Upvotes: 0

Views: 28

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100533

You can try

Option 1:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return carsArray.count + 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   if indexPath.row < carsArray.count {
    let rowData = carsArray[indexPath.row]

    let cell = tableView.dequeueReusableCell(withIdentifier: "carCell") as! CarCell
    cell.setButton(name: rowData.name)

    return cell
   }
   else {

     // implement the last cell 
  }
}

Option 2:

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let footerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 50))
    return footerView
}

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 50
}

Upvotes: 1

Related Questions