简体   繁体   English

如何添加两个标签作为空的UITableView背景,而不是迅速添加一个标签?

[英]How can I add two labels as an empty UITableView background instead of one in swift?

I have a UITableViewController filled with data dynamically. 我有一个UITableViewController动态填充数据。 Now I want to show a message that says there is no data in case there is no data. 现在,我想显示一条消息,说如果没有数据,则没有数据。 I want to add a longer message, so I've decided to add two labels since I don't know how to make a 2-level uilabel. 我想添加一条更长的消息,所以我决定添加两个标签,因为我不知道如何制作2级uilabel。 This is my code so far: 到目前为止,这是我的代码:

override func tableView(tview: UITableView, numberOfRowsInSection section: Int) -> Int {
        var emptyLabel = UILabel(frame: CGRectMake(0, 10, self.view.bounds.size.width, 10))//self.view.bounds.size.height))
        var emptyLabel2 = UILabel(frame: CGRectMake(0, -10, self.view.bounds.size.width, 10))//self.view.bounds.size.height))
        emptyLabel.text = "There is no data over here."
        emptyLabel.font = emptyLabel.font.fontWithSize(10)
        emptyLabel2.text = "Please pull down the list to refresh or come back later"
        emptyLabel2.font = emptyLabel.font.fontWithSize(10)
        tview.backgroundView = emptyLabel
        tview.backgroundView = emptyLabel2
        if self.items.count == 0{
            emptyLabel.hidden = false
            emptyLabel2.hidden = false
            tview.separatorStyle = UITableViewCellSeparatorStyle.None
            return 0
        } else {
            emptyLabel.hidden = true
            emptyLabel2.hidden = true
            tview.separatorStyle = UITableViewCellSeparatorStyle.None
        return self.items.count;
        }
    }

But after doing so, I can only see the 2nd label. 但是这样做之后,我只能看到第二个标签。 How can I display both of them in the center of the screen, one above the other? 如何在屏幕中央显示它们两个?

You only see second label because of these 2 lines of code: 由于这两行代码,您只能看到第二个标签:

    tview.backgroundView = emptyLabel
    tview.backgroundView = emptyLabel2

Here you override first label with the second. 在这里,您用第二个覆盖第一个标签。 Only the second one appears as a result. 结果只有第二个出现。 However, there is lots of problems in your code. 但是,您的代码中存在很多问题。 Let's tackle them one by one. 让我们一一解决。

  1. Make the label multiline. 使标签为多行。 Use numberOfLines property to make the label multiline. 使用numberOfLines属性使标签变为多行。

     emptyLabel.numberOfLines = 0 // Setting 0 makes the label multiline 
  2. You'll need to make it taller though to fit 2 lines of text so when initializing the label provide bigger frame: 您需要使其更高,以适合两行文本,因此在初始化标签时提供更大的框架:

     var emptyLabel = UILabel(frame: CGRectMake(0, 10, self.view.bounds.size.width, 100)) 
  3. Don't create views in numberOfRowsInSection method. 不要在numberOfRowsInSection方法中创建视图。 This is a wrong place. 这是一个错误的地方。 Do it rather in viewDidLoad . 宁可在viewDidLoad numberOfRowsInSection might be called multiple times and you will end up with recreating views every time the method is called. 可能会多次调用numberOfRowsInSection ,并且每次调用该方法时,您最终都会重新创建视图。

var emptyLabel2 = UILabel(frame: CGRectMake(0, -10, self.view.bounds.size.width, 10))

is setting the y origin to -10, which will not show up since it's height is only 10. 将y原点设置为-10,由于高度仅为10,因此不会显示。

I think all you will need to o is set it to 0. 我认为将o所需的全部设置为0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 swift 在 UITableView 部分标题中添加图像? - How can I add image in UITableView section header using swift? 如何将两个UIImages添加到UITableView Background - How to add two UIImages to UITableView Background 如何用两个查询填充一个 UITableView? - How can I populate one UITableView with two queries? 如何在一个UIViewController中添加多个UITableView? - How can I add more than one UITableView in a UIViewController? 如何在一个Uitableview中添加两个笔尖UITableViewCell - How to add a two nib UITableViewCell in one Uitableview 在Swift中使用UITableView填充单元格之前,如何在该数组中添加CKRecords? - How can I add CKRecords to an array before using that array to fill cells in a UITableView in Swift? 我有一个 UILabel 仅当我的 UITableView 为空时才可见。 如何在 Swift 中将该标签的文本居中? - I have a UILabel that is visible only when my UITableView is empty. How can I center the text of that label in Swift? 如何使用Swift iOS在Mapview中添加两个注释? - How can I add two annotations in mapview using swift ios? 我如何在 swift 3 的一个视图控制器中填充两个不同的表视图 - How i can fill two different tableview in one viewcontroller in swift 3 如何快速在 UIAlertView 中添加 UITableView - How to add UITableView in a UIAlertView in swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM