简体   繁体   English

如何在 UITableView 底部加载第二个 UITableViewCell?

[英]How to load second UITableViewCell at the bottom of UITableView?

I have a tableView which populates custom UITableViewCells which I call " TopCell ".我有一个tableView ,它填充我称之为“ TopCell ”的自定义 UITableViewCells。 Everything works flawlessly.一切都完美无缺。 However, I would like to add a second custom UITableViewCell to be displayed at the bottom of the tableView .但是,我想添加第二个自定义 UITableViewCell 以显示在tableView的底部。 Lets call it " BottomCell "让我们称之为“ BottomCell

I already created BottomCell and connected to its custom class.我已经创建了BottomCell并连接到它的自定义类。 Just like I did with the " TopCell "就像我对“ TopCell ”所做的TopCell

Bottom cell will displayed only once and at the bottom of the tableView .底部单元格将仅显示一次并且位于tableView的底部。

Here below is the basic code for TopCell .下面是TopCell的基本代码。 So how to integrate BottomCell ?那么如何集成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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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