ios - 如果 UITableView 没有数据,如何添加 UIImage 和 UILabel

标签 ios uitableview swift3 uiimage

我已经研究了很多答案,但要么只是 UILabel 要么是 UIImage 而不是两者。所以在尝试实现之后我终于发现我们不能做两个tableView.backgroundView。这是我到目前为止所做的:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        let noDataLabel: UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: tableView.bounds.size.height))

let image = UIImage(named: "noData")
let noDataImage = UIImageView(image: image)

noDataImage.frame = CGRect(x: 0, y: 10, width: 20, height: 20)

if allData.count == 0 {

noDataLabel.isHidden = false
noDataImage.isHidden = false

noDataLabel.text          = "No data added. Add new entry \nby pressing the add icon on top right."
noDataLabel.textColor     = UIColor.black
noDataLabel.numberOfLines = 3
noDataLabel.backgroundColor = UIColor.white
noDataLabel.textAlignment = .center

 //what to do from here           
            tableView.backgroundView  = noDataImage
            tableView.backgroundView  = noDataLabel

//end
                tableView.separatorStyle  = .none
                return 0;
            }
            else {
                noDataLabel.backgroundColor = UIColor.clear
                noDataLabel.isHidden = true
                noDataImage.isHidden = true
                tableView.backgroundView  = nil
                return allData.count
            }

我想显示一张图片,我想在该图片下方显示一个 UILabel。我怎样才能做到这一点?

最佳答案

您需要创建一个包含 subview 、您的图像标签 View

var backgroundView =UIView(frame: CGRectMake(0, 0, your_width, your_height))
backgroundView.addSubview(noDataImage)
backgroundView.addSubview(noDataLabel)

 tableView.backgroundView=backgroundView;

注意:根据您的使用调整noDataImagenoDataLabel的框架。

关于ios - 如果 UITableView 没有数据,如何添加 UIImage 和 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44521464/

相关文章:

iOS 7 Chrome - 当虚拟键盘弹出时,固定位置抽屉不会停留在底部

ios - 具有动态高度的单元格 Accordion 式动画的 UITableView

ios - 使用dequeueReusableCellWithIdentifier ios将单元格中的ImageView绑定(bind)到错误的行号

swift - NSKeyedUnarchiver.unarchiveObject() 取消归档旧对象

objective-c - UITableView scrollToRowAtIndexPath 不适用于最后 4

ios - FGallery 覆盖了我其他 View 中的所有旋转方法

ios - 选择时 UISearchBar 的宽度和位置会发生变化

ios - 点击按钮出现错误: unrecognized selector sent to instance - IOS Swift 3

ios - 过滤 TableView Controller.reloadRows 仅在第一次调用时重新加载行

ios - 将外部框架连接到 UI 单元测试目标,iOS