【问题标题】:How do I can upload label under the sections by using footer in the TableView in the Swift?如何在 Swift 的 TableView 中使用页脚在部分下上传标签?
【发布时间】:2020-03-31 23:10:05
【问题描述】:

堆栈溢出流。

我知道这些页脚可能*有很多方法可以在单元格下制作标签作为已知页脚。

我知道如何使用 index.row 创建页脚,它允许您使用数组自动标题、图像和所有内容,否则页脚不会?

我正在尝试在 iOS 13 中设置样式等部分下制作标题。

资源代码:

当您创建一个数组以提供自动单元格时。

class SettingViewController: UIViewController {

  var settingtitle = ["Item1", "Item2", "Item3"]

  var footerTitle = ["Footer1", Footer2", Footer3"]

  @IBOutlet weak var TableView: UITableView!

}

现在是时候使用 Delegate 和 Data Source 构建 UITableView 了。

extension SettingViewController: UITableViewDelegate, UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
 return settingtitle.count

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

 let cells = TableView.dequeueReusableCell(withIdentifier: "Cells", for: indexPath) as? SettingTableViewCell

 cells?.Setting_Title.text = settingtitle[indexPath.row]

 return cells!
 }
}

但是我怎样才能将标签与数组一起上传到单元格下方?

我尝试了许多解决堆栈溢出的方法,但我注意到他们的一种方法有效,但它显示在整个单元格的一个页脚下。

感谢您对这个问题的帮助。 如果你知道,请告诉我。

【问题讨论】:

    标签: arrays swift uitableview uilabel footer


    【解决方案1】:

    实现这个方法:

    func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
        return footerTitle[section]
    }
    

    === 已编辑 ===

    如果要为每个单元格设置页脚:

      func numberOfSections(in tableView: UITableView) -> Int {
        return settingtitle.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        let cells = TableView.dequeueReusableCell(withIdentifier: "Cells", for: indexPath) as? SettingTableViewCell
        cells?.Setting_Title.text = settingtitle[indexPath.section]
        return cells!
    }
    
    func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
        return footerTitle[section]
    }
    

    【讨论】:

    • 我测试了该方法已为整个细胞显示一个页脚。 :(
    【解决方案2】:

    第一组委托:
    TableView.delegate = self

    然后添加以下方法: func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { 让 footerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 100)) footerView.backgroundColor = .green

    return footerView
    

    }

    【讨论】:

    • 这是一个 UIView 出现在单元格的下方是完美的,但它需要像这样出现在每个单元格下方的数组。
    • 所以你想要一个单元格,然后是页脚,然后是第二个单元格,然后是页脚吗?
    • 是的,这就是我要找的东西。对这样的设置很熟悉。
    猜你喜欢
    • 2020-04-09
    • 2018-12-03
    • 2016-12-02
    • 2021-06-27
    • 1970-01-01
    • 2015-05-27
    • 2015-07-20
    • 2018-08-21
    相关资源
    最近更新 更多