Swift - 自定义视图以显示空表格单元格

时间:2017-02-28 00:58:48

标签: ios swift swift3

我使用的是Swift 3,Xcode 8.2。

当无法显示时,我已经能够创建一个标签来覆盖空表视图单元格。

我的代码如下,它位于sudo su

的子类中
UITableViewController

结果如下。

enter image description here

看起来很好。但是,如何使其包含另一行文本,向下箭头指向凸起的中心按钮。像“点击这里开始扫描”这样的东西?

我尝试在override func numberOfSections(in tableView: UITableView) -> Int { // if there are scans to display... if items.count > 0 { tableView.backgroundView = nil tableView.separatorStyle = .singleLine return 1 } else { // otherwise, return 0, remove cell lines, and display a Label let rect = CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: tableView.bounds.size.height) let noScanLabel: UILabel = UILabel(frame: rect) noScanLabel.text = "No Scans" noScanLabel.textColor = UIColor.gray noScanLabel.font = UIFont.boldSystemFont(ofSize: 24) noScanLabel.textAlignment = NSTextAlignment.center tableView.backgroundView = noScanLabel tableView.separatorStyle = .none return 0 } } 字段中添加新的换行符但是没有用。任何正确方向的指针都会有所帮助。

2 个答案:

答案 0 :(得分:4)

简单的解决方案是在numberOfLines上将0设置为noScanLabel。这样,新行将显示。

let noScanLabel: UILabel = UILabel(frame: rect)

noScanLabel.text = "No Scans"
noScanLabel.textColor = UIColor.gray
noScanLabel.font = UIFont.boldSystemFont(ofSize: 24)
noScanLabel.numberOfLines = 0

noScanLabel.textAlignment = NSTextAlignment.center

请注意,在这种情况下,为了更好的可维护性,我建议实际从TableView删除UIViewController(因此不会从UITableViewController继承)并将其替换为空检测到没有可用的扫描时查看。这将使每个州彼此更加独立,并使维护更容易。

答案 1 :(得分:1)

有几种方法可以实现您的目标。有一个名为DZNEmptyDataSet的知名库,用于处理空的tableviews和collectionviews。 https://github.com/dzenbot/DZNEmptyDataSet

另一种方法是使用指定的rect创建一个uiview,然后为该uiview添加两个标签。一个是你的noScanLabel,另一个是包含你的箭头的标签或图像。您可以根据需要设置布局约束,以便箭头指向下方。

此代码似乎运行良好。如果需要,可以更改约束

   let rect = CGRect(x: 0, y: 0, width: tableview.bounds.size.width, height: tableview.bounds.size.height)
    let noDataView = UIView(frame: rect)
    let noScanLabel = UILabel()
    noScanLabel.text = "No Scans"
    noScanLabel.textColor = UIColor.gray
    noScanLabel.font = UIFont.boldSystemFont(ofSize: 24)
    noScanLabel.textAlignment = NSTextAlignment.center
    let arrowLabel = UILabel()
    arrowLabel.text = "Add Arrow Image to this label"
    arrowLabel.textColor = UIColor.gray
    arrowLabel.font = UIFont.boldSystemFont(ofSize: 24)
    arrowLabel.textAlignment = NSTextAlignment.center

    noScanLabel.widthAnchor.constraint(equalToConstant: 100)
    arrowLabel.widthAnchor.constraint(equalToConstant: 50)
    noDataView.addSubview(noScanLabel)
    noDataView.addSubview(arrowLabel)

    arrowLabel.translatesAutoresizingMaskIntoConstraints = false
    noDataView.translatesAutoresizingMaskIntoConstraints = false
    noScanLabel.translatesAutoresizingMaskIntoConstraints = false


    self.tableview.addSubview(noDataView)
    noDataView.isHidden = false
    noDataView.centerXAnchor.constraint(equalTo: self.tableview.centerXAnchor).isActive = true
    noDataView.centerYAnchor.constraint(equalTo: self.tableview.centerYAnchor).isActive = true

    noScanLabel.centerXAnchor.constraint(equalTo: noDataView.centerXAnchor).isActive = true
    noScanLabel.topAnchor.constraint(equalTo: noDataView.centerYAnchor).isActive = true

    arrowLabel.centerXAnchor.constraint(equalTo: noDataView.centerXAnchor).isActive = true
    arrowLabel.topAnchor.constraint(equalTo: noScanLabel.bottomAnchor).isActive = true

另一种选择是将线数设置为零,如上所述

noScanLabel.numberLines = 0