【问题标题】:Empty UITableView with tableFooterView带有 tableFooterView 的空 UITableView
【发布时间】:2019-05-02 10:55:09
【问题描述】:

我有一个UITableView,当dataSource 为空时,我想在其中显示一条消息。我通过使用以下扩展名设置 backgroundView 的众所周知的方法来做到这一点:

extension UITableView {

    func setEmptyMessage(_ message: String, _ image: String) {
        let emptyView: UIView = {
            let emptyView = UIView(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height))
            return emptyView
        }()

        let contentView: UIView = {
            let contentView = UIView()
            contentView.translatesAutoresizingMaskIntoConstraints = false
            return contentView
        }()

        let messageLabel = UILabel()
        let messageCommentStyle = NSMutableParagraphStyle()
        messageCommentStyle.lineHeightMultiple = 1.2

        let attributedString = NSMutableAttributedString(string: message)
        attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: lightFeedUserNameFontColor, range: NSRange(location: 0, length: attributedString.length))
        attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: messageCommentStyle, range: NSRange(location: 0, length: attributedString.length))
        attributedString.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: normalFontSize), range: NSRange(location: 0, length: attributedString.length))

        messageLabel.attributedText = attributedString
        messageLabel.numberOfLines = 0
        messageLabel.font = UIFont.systemFont(ofSize: normalFontSize)
        messageLabel.textAlignment = .center
        messageLabel.sizeToFit()
        messageLabel.translatesAutoresizingMaskIntoConstraints = false

        let errorImage: UIImageView = {
            let errorImage = UIImageView()
            errorImage.translatesAutoresizingMaskIntoConstraints = false
            return errorImage
        }()

        self.backgroundView = emptyView

        emptyView.addSubview(contentView)
        contentView.addSubview(errorImage)
        contentView.addSubview(messageLabel)

        contentView.centerYAnchor.constraint(equalTo: emptyView.centerYAnchor).isActive = true
        contentView.centerXAnchor.constraint(equalTo: emptyView.centerXAnchor).isActive = true
        contentView.leadingAnchor.constraint(equalTo: emptyView.leadingAnchor, constant: normalSpacing * 3).isActive = true
        contentView.trailingAnchor.constraint(equalTo: emptyView.trailingAnchor, constant: -(normalSpacing * 3)).isActive = true
        contentView.topAnchor.constraint(equalTo: errorImage.topAnchor).isActive = true
        contentView.bottomAnchor.constraint(equalTo: messageLabel.bottomAnchor).isActive = true

        messageLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
        messageLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true
        messageLabel.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
    }

    func restore() {
        self.backgroundView = nil
    }
}

我是这样设置的:

if(tableData.isEmpty) {
    self.tableView.setEmptyMessage("No results!", "none")
} else {
    self.tableView.restore()
}

self.tableView.reloadData()

没什么大不了的,我们都看过这个并且可能使用过它。而且效果很好。但是,我现在在 tableFooterView 的底部有一个UIButton。这个按钮保持在UITableView 的顶部,因为它会自动将自己定位在最后一个单元格的正下方,这正是我想要的,当有数据时,但是现在屏幕中间显示了空消息,而按钮在上面它。我该如何解决这个问题,以便在 dataSource 为空时有一种框架?

为了说明:

【问题讨论】:

  • 当数据源为空时,不要使用 backgroundView,而是返回 1 个表格视图单元格并显示无结果消息
  • 当数据为空..时,添加tblView.footerView = UIView()

标签: ios swift uitableview tablefooterview


【解决方案1】:

所以现在您将 tableView backgroundView 设置为 emptyView。您可以查看您的数据源,如果它为空 - 只需使用此消息返回新单元格以获取空状态。

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return dataSource.count > 0 ? dataSource.count : 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if dataSource.count.isEmpty 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "EmptyCell")
        cell.textLabel.text = "No Results!"
        return cell
    } else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SomeCell")
        return cell
    }
}

【讨论】:

  • 我已经尝试过这种方法,但现在我收到以下错误:无效更新:无效的节数。更新后表视图中包含的节数(1)必须等于更新前表视图中包含的节数(1),加上或减去插入或删除的节数(1插入,0已删除)。 当我插入一个新部分时。发生可能是因为 tableData (dataSource) 一开始是空的,但后来我只附加了一项。即使表格是空的,我的 tableData 中是否应该有一项?
  • 您不需要插入新部分。您可以使用 tableView.reloadData() 重新加载您的 tableView
  • 您的部分数量仍为 1。根据您的数据源是否为空,您返回项目的单元格或空单元格
  • 在插入(或删除)部分时,我调用 beginUpdates 和 endUpdates 并在其中包含动画插入块,因此在添加或删除部分时不使用 reloadData()。当 dataSource 计数变为 1 时,我将尝试调用 reloadData(),否则调用插入块。
  • 试试 tableView.reloadData() 或 reloadRowsAtIndexPaths developer.apple.com/documentation/uikit/uitableview/…
【解决方案2】:

写一个扩展

extension UITableView
{
    func addErrorMessageLabel(noDataText:String = "No data available")->UILabel
    {
        let noDatalabel:UILabel!
        noDatalabel=UILabel(frame: CGRect(x: self.frame.size.width/2-200, y: self.frame.size.height/2-50, width: 400, height: 100))
        noDatalabel.textColor = textThemeColor
        noDatalabel.text=noDataText
        noDatalabel.numberOfLines=0
        noDatalabel.textAlignment = .center
        noDatalabel.isHidden=true
        self.addSubview(noDatalabel)
        self.alignCenterToSuperView(item: noDatalabel, horizentally: true, vertically: true,height: 100,width: 400)
        return noDatalabel
    }
    func alignCenterToSuperView(item:UIView,horizentally:Bool,vertically:Bool , height:Int, width:Int)
{
    if horizentally
    {
        item.translatesAutoresizingMaskIntoConstraints = false
        let xConstraint = NSLayoutConstraint(item: item, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0)
        NSLayoutConstraint.activate([xConstraint])
    }
    if vertically
    {
        item.translatesAutoresizingMaskIntoConstraints = false
        let yConstraint = NSLayoutConstraint(item: item, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0)
        NSLayoutConstraint.activate([yConstraint])
    }
    let Height = NSLayoutConstraint(item: item, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant:CGFloat(height))
    item.addConstraint(Height)
    let Width = NSLayoutConstraint(item: item, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant:CGFloat(width))
    item.addConstraints([Width,Height])
}
}

在你的 ViewController 中创建

    var noDataLabel:UILabel!

在你的 ViewDidLoad 中

 override func viewDidLoad() {
    super.viewDidLoad()
    noDataLabel = tableView.addErrorMessageLabel()
    noDataLabel.text = "No data found" // or your message
}

最后一步

    if(tableData.isEmpty) {
    noDataLabel.isHideen = false
} else {
    noDataLabel.isHideen = true
}

【讨论】:

    猜你喜欢
    • 2012-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-17
    • 2010-11-24
    • 1970-01-01
    • 2013-10-24
    • 1970-01-01
    相关资源
    最近更新 更多