I have a UITableView
in my app and I want to present there a UILabel
when there is no content. So far I'm doing:
@IBOutlet var tview: UITableView!
var emptyLabel = UILabel(frame: CGRectMake(0, 10, 100, 100))
override func viewDidLoad(){
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: "refresh:", forControlEvents: .ValueChanged)
refreshControl.attributedTitle = NSAttributedString(string: "last updated on \(NSDate())")
tview.separatorStyle = UITableViewCellSeparatorStyle.None
tview.addSubview(refreshControl)
emptyLabel.numberOfLines = 0
emptyLabel.text = "There is no content in this table for now. Please pull down the list to refresh and something should appear"
emptyLabel.font = emptyLabel.font.fontWithSize(10)
tview.backgroundView = emptyLabel
}
But when I do like that, I have the following result:
and instead of the alignment to the left I would like to center the text, so that it looks something like:
There is no content in this table for now. Please pull down the
list to refresh and something should appear
Also, currently it's centered vertically - is there a way of putting this message let's say in 1/3 of the screen from the top?
====== EDIT
@Md.Muzahidul Islam this is how I present the label when the table is empty:
override func tableView(tview: UITableView, numberOfRowsInSection section: Int) -> Int {
if self.items.count == 0{
self.emptyLabel.hidden = false
return 0
} else {
self.emptyLabel.hidden = true
return self.items.count;
}
}
You can achieve this by:
override func viewDidLoad()
{
// Other codes
emptyLabel = UILabel(frame: CGRectMake(0, 0, tview.bounds.size.width, view.bounds.size.height))
emptyLabel.textAlignment = NSTextAlignment.Center
emptyLabel.numberOfLines = 0
emptyLabel.text = "There is no content in this table for now. Please pull down the list to refresh and something should appear"
emptyLabel.font = emptyLabel.font.fontWithSize(10)
tview.backgroundView = emptyLabel
}
You can read more about it here
Swift3.0
I hope it server your purpose...... In your UITableViewController .
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchController.isActive && searchController.searchBar.text != "" {
if filteredContacts.count > 0 {
self.tableView.backgroundView = .none;
return filteredContacts.count
} else {
Helper.EmptyMessage(message: ConstantMap.NO_CONTACT_FOUND, viewController: self)
return 0
}
} else {
if contacts.count > 0 {
self.tableView.backgroundView = .none;
return contacts.count
} else {
Helper.EmptyMessage(message: ConstantMap.NO_CONTACT_FOUND, viewController: self)
return 0
}
}
}
Helper Class with function :
/* Description: This function generate alert dialog for empty message by passing message and
associated viewcontroller for that function
- Parameters:
- message: message that require for empty alert message
- viewController: selected viewcontroller at that time
*/
static func EmptyMessage(message:String, viewController:UITableViewController) {
let messageLabel = UILabel(frame: CGRect(x: 0, y: 0, width: viewController.view.bounds.size.width, height: viewController.view.bounds.size.height))
messageLabel.text = message
let bubbleColor = UIColor(red: CGFloat(57)/255, green: CGFloat(81)/255, blue: CGFloat(104)/255, alpha :1)
messageLabel.textColor = bubbleColor
messageLabel.numberOfLines = 0;
messageLabel.textAlignment = .center;
messageLabel.font = UIFont(name: "TrebuchetMS", size: 18)
messageLabel.sizeToFit()
viewController.tableView.backgroundView = messageLabel;
viewController.tableView.separatorStyle = .none;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.