简体   繁体   中英

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
}

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM