2017-02-28 96 views
0

我正在使用Swift 3,Xcode 8.2。Swift - 自定义视图以显示空表格单元

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

我的代码如下,它位于UITableViewController的子类中。

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 
    } 
} 

这是结果。

enter image description here

看起来不错。但是,如何做到这样,我可以将另一行文本包含在向下箭头中,指向凸起的中心按钮。就像“点击这里开始扫描”?

我已经尝试在noScanLabel.text字段中添加新的行字符,但这并没有解决。任何指向正确方向的指针都会有所帮助。

+0

请您详细说明一下吗? – noblerare

回答

4

简单的解决办法是在noScanLabel上设置numberOfLines0。这样,新的线将显示。

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 

注意,除了在这种情况下,我建议,为了更好的维护,实际上从UIViewController删除TableView(因此不能从UITableViewController继承),并与空视图替换它,当你发现没有扫描可用。这将使每个国家更加独立,并使维护更容易。

+0

这对我很有帮助。所以如果我做了“No Scans \ n \ n \ n这里的副标题”如果我想让副标题使用不同的字体大小怎么办? – noblerare

+0

你有两种选择:使用'NSFormattedString'或者,这就是我推荐的,使用第二个标签配置你想要的方式。 – tomahh

+0

我想使用第二个标签,但我只能将'tableView.backgroundView'设置为一个'UILabel'。 – noblerare

1

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

另一种方法是用您指定的矩形创建一个uview,然后为该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 
0

你可以采取的UIView并添加UIView的所有的UILabel和箭头图像,然后赋值给行数设置为零改变约束UIView到TableView的backgroundView。

像这样。

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 messageBaseView = UIView(frame: rect) 

     //Add your first label.. 
     let noScanLabel: UILabel = UILabel() 
     noScanLabel.text = "No Scans" 
     noScanLabel.textColor = UIColor.gray 
     noScanLabel.font = UIFont.boldSystemFont(ofSize: 24) 
     noScanLabel.textAlignment = NSTextAlignment.center 
     messageBaseView.addSubView(noScanLabel) 

     //Add your second label.. and your arrow image here on messageBaseView 

     //Assign messageBaseView to backgroundView of tableView 
     tableView.backgroundView = messageBaseView 
     tableView.separatorStyle = .none 



     return 0 
    } 
} 
相关问题