Vetuka
Vetuka

Reputation: 1661

How to load second UITableViewCell at the bottom of UITableView?

I have a tableView which populates custom UITableViewCells which I call "TopCell". Everything works flawlessly. However, I would like to add a second custom UITableViewCell to be displayed at the bottom of the tableView. Lets call it "BottomCell"

I already created BottomCell and connected to its custom class. Just like I did with the "TopCell"

Bottom cell will displayed only once and at the bottom of the tableView.

Here below is the basic code for TopCell. So how to integrate BottomCell?

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "TopCell", for: indexPath) as? TopCell else { return UITableViewCell() }

    let topVal = indexPath.row + 1
    let noVal = myData[indexPath.row].no ?? 0

    cell.configureCell(top: topVal, no: noVal)
    return cell
}

Upvotes: 1

Views: 189

Answers (1)

Amit
Amit

Reputation: 4896

Is this what you are asking for :

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

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

    if indexPath.row == myData.count {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "BottomCell", for: indexPath) as? BottomCell else { return UITableViewCell() }

        return cell
    }
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "TopCell", for: indexPath) as? TopCell else { return UITableViewCell() }

    let topVal = indexPath.row + 1
    let noVal = myData[indexPath.row].no ?? 0

    cell.configureCell(top: topVal, no: noVal)
    return cell
}

Upvotes: 4

Related Questions