Swift 如何在 tableview 之间显示单独的 View(box)

How to show separate View(box) in between tableview in Swift

在此屏幕中如何在表视图行之间显示(蓝色视图)

design image

代码: 在故事板设计中,我已经在标签和图像中给出了所有静态数据,因此使用下面的代码我得到了所有像上面屏幕截图一样的单元格,但是在三个单元格之后如何要显示蓝框视图,请建议我

import UIKit

class ViewController: UIViewController , UITableViewDataSource, UITableViewDelegate {



func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return 5
} 

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

有两种方法可以做到这一点:

  1. 使用不同的 UITableViewCell class(可能是您正在寻找的?)
  2. 使用部分

如何?

您可以在情节提要中创建新的 UITableViewCell 原型单元格,也可以通过编程方式创建。 像这样创建自定义 UITableViewCell:

class OfferTableViewCell: UITableViewCell {

}

// If you are not using storyboards, add the following code
// in your viewDidLoad
tableView.register(OfferTableViewCell.self, forCellReuseIdentifier: "your_cell_id")

然后,您可以在任何索引处将新创建的单元格出队:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.row == 10 // Index of the different cell
        let cell = tableView.dequeueReusableCell(withIdentifier: "your_cell_id", for: indexPath) as! OfferTableViewCell
        // Do cell configuration here
        return cell
    } else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "BidCell", for: indexPath)
        return cell
    }
}

请记住,如果您使用数组作为数据源,此单元格将取代另一个单元格,因此使用 myArray.count 作为您的 numberOfRowsInSection 将导致 缺少最后一个数组元素。你必须考虑到这一点。

资源