我有一个UILabel只有在我的UITableView为空时才可见。如何在Swift中居中标签的文本?

时间:2016-03-20 12:04:20

标签: ios swift uitableview uilabel

我的应用中有一个UITableView,当没有内容时,我希望在那里显示UILabel。到目前为止,我正在做:

@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
}

但是当我这样做时,我得到以下结果:

enter image description here

而不是左边的对齐,我想将文本居中,以便它看起来像:

       There is no content in this table for now. Please pull down the
                list to refresh and something should appear

此外,目前它是垂直居中的 - 是否有一种方法可以将此消息放在屏幕的1/3处?从顶部开始?

======编辑

@ Md.Muzahidul Islam这是我在桌子为空时展示标签的方式:

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;
    }
}

2 个答案:

答案 0 :(得分:1)

您可以通过以下方式实现这一目标:

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
}

您可以阅读更多相关信息here

答案 1 :(得分:0)

<强> Swift3.0

我希望它服务于你的目的...... 在你的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
        }
    }
}

具有功能的助手类:

 /* 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;
        }