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



如何在tableview行之间显示(蓝色视图)

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

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类(可能你正在寻找什么?)
  2. <
  3. 使用部分/gh>

如何?

你可以在你的故事板中创建一个新的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将导致最后一个数组元素缺失。你必须考虑到这一点。

  • 在TableView中使用多个自定义单元格
  • <
  • UITableView部分/gh>
  • TableView section的自定义头