【问题标题】:How to show separate View(box) in between tableview in Swift如何在 Swift 中的 tableview 之间显示单独的 View(box)
【发布时间】:2021-09-29 16:43:03
【问题描述】:

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

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
}

【问题讨论】:

    标签: swift tableview cell


    【解决方案1】:

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

    1. 使用不同的 UITableViewCell 类(可能是您要找的?)
    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 会导致 最后一个数组元素丢失时间>。您必须考虑到这一点。

    资源

    【讨论】:

    • 我正在做你的第一个选项.. 但是当我使用两个原型单元格时,我得到第二个单元格高度也是第一个单元格高度.. 如何更改第二个单元格高度.. NOTE: i am not using sections to change height
    • heightForRowAt 中的@User123 对indexPath.row 的值执行相同的检查。我建议你对estimatedHeightForRowAt做同样的事情
    猜你喜欢
    • 1970-01-01
    • 2017-10-22
    • 1970-01-01
    • 2016-03-16
    • 1970-01-01
    • 2015-04-26
    • 1970-01-01
    • 1970-01-01
    • 2017-04-08
    相关资源
    最近更新 更多