[英]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.
让我们一一解决。
Make the label multiline. 使标签为多行。 Use
numberOfLines
property to make the label multiline. 使用
numberOfLines
属性使标签变为多行。
emptyLabel.numberOfLines = 0 // Setting 0 makes the label multiline
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))
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.