【问题标题】:Static cell at the bottom of a UITableViewUITableView 底部的静态单元格
【发布时间】:2017-11-23 00:47:36
【问题描述】:

我有一个包含联系人的表格视图。在表格视图的最底部(在最后一次联系之后),我希望有一个静态表格视图单元格,该单元格始终存在自定义内容。

我怎样才能做到这一点?

【问题讨论】:

  • 创建一个自定义UIView 并将其设为tableFooterView。就是这样
  • 好的。所以你是说我应该在里面放一个带有按钮的 UIView 并用那个 tableFooterView 让它到底部?我如何以这种方式设置约束?
  • 没错!它很简单而且效果很好。但是你需要实现一些UITableViewDelegate 方法
  • 很抱歉这是一个愚蠢的问题,但是如何将约束添加到所有表格视图单元格的底部?
  • 非常感谢!!

标签: ios uitableview cocoa-touch


【解决方案1】:

示例如下:

override func viewDidLoad() {
    super.viewDidLoad()

    let button = UIButton.init()
    button.frame = CGRect.init(x: 0, y: 0, width: 100, height: 44)
    button.setTitle("✚", for: .normal)
    button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 30)
    button.setTitleColor(UIColor.darkGray, for: .normal)
    button.addTarget(self, action: #selector(handle(sender:)), for: .touchUpInside)

    self.tableView.tableFooterView = button
}

及相关功能:

func handle(sender: UIButton) {
    print("Tapped")
}

结果如下:

另一种方式!我们可以使用UITableViewDelegate 来实现。下面是我们如何完成的示例。

    override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {

        let button = UIButton.init()
        button.frame = CGRect.init(x: 0, y: 0, width: 100, height: 44)
        button.setTitle("✚", for: .normal)
        button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 30)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.addTarget(self, action: #selector(handle(sender:)), for: .touchUpInside)

        return button
    }

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


    func handle(sender: UIButton) {
        print("Tapped")
    }

注意:第二个选项在主屏幕上始终可见,就像部分标题一样。

【讨论】:

    【解决方案2】:

    假设您用来填充UITableView的数组称为“contactsArray”。

    在下面的委托方法中你需要做的是添加一个额外的行,例如:

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
        return contactsArray+1
    
    }
    

    然后你需要处理这个逻辑,例如:

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    if indexPath.row >= contactsArray.count{
            print("Last Row")
        }
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-31
      • 1970-01-01
      相关资源
      最近更新 更多