ios - 如何在表格中显示自定义页脚 View ?

标签 ios swift uitableview tablefooterview

我创建了一个 UIviewXib 以在我的表格的页脚 View 中显示它。 FooterView

import UIKit

/// This class is to display footer view in a settings table view
class FooterView: UIView {
    override func awakeFromNib() {
        super.awakeFromNib()
    }
}

我显示如下:

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let view = FooterView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 140))
    return view
}

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 100
}

这是怎么回事?我无法看到页脚 View ,也无法在 tableview 行之间获得如此大的空间。

enter image description here

最佳答案

使用 Storyboard

UITableView中可以拖动UIView,如果原型(prototype)cell超过0个,它会设置为FooterView。拖动后,您可以在 TableView 层次结构中看到它作为 subview 。现在,您可以添加 UILabel UIButton 或该 View 上的任何组件,调整高度等。您还可以将 IBAction 设置到 ViewController 类文件中.

以编程方式

let customView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
customView.backgroundColor = UIColor.red
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
button.setTitle("Submit", for: .normal)
customView.addSubview(button)

//Add that view in Table Footer View.
tableView.tableFooterView = customView // Here you can also use your xib View.

通过使用 XIB

class MyClass: UIView {

    class func instanceFromNib() -> UIView {
        return UINib(nibName: "nib file name", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView
    }
}

初始化 View 并像下面那样使用它

let FooterView = MyClass.instanceFromNib()
tableView.tableFooterView = FooterView

enter image description here

输出:

enter image description here

关于ios - 如何在表格中显示自定义页脚 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52108871/

相关文章:

javascript - React Native IOS 生产版本 : RCTFatalException crash and SocketProtocolError YellowBox

ios - 使用自定义 UIWindow 时第一响应者的问题

ios - "no index path for table cell being reused"导致应用崩溃

ios - Self Sizing CollectionView inside a Self Sizing TableViewCell

iphone - 防止 iPhone 进入休眠状态

ios - textView.typingAttributes 不适用于预测

ios - SWIFT iOS 中未调用委托(delegate)

ios - 3D触摸力有单位吗?

ios - 无法使用类型为 'performSelector' 的参数列表调用 '(String)'

swift - UITableView 中的 UICollectionView 由于 view.addGestureRecognizer() 未检测到点击