ios - 将底部单元格添加到动态表格 View

标签 ios swift uitableview

我有一个带有动态单元格的 TableView :

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
    }
}

它工作正常,但我如何在表格 View 的底部添加一个包含自定义内容的单元格?

最佳答案

你可以试试

选项 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 
  }
}

选项 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
}

关于ios - 将底部单元格添加到动态表格 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56203287/

相关文章:

ios - 用户输入不适用于在表格 View 中以编程方式添加的文本字段

ios - UIActivityViewController仅具有文本和邮件

swift - 绘制 View : the drawing is not in the same position as my finger or my cursor

ios - swift 3 错误 [_SwiftValue nsli_superitem]

ios - 快速开发 : How can I check if my local development api server is running?

swift - 滑动时切换 TableViewCell 中框的颜色

UITableViewCell 删除按钮被覆盖

ios - AirPlay iOS 接收器/录音机

iphone - 初学者如何在 ios 中使用 PFQuery

ios - Mantle - 在 Swift 中转换嵌套模型