If no Table View results, display “No Results” on screen

后端 未结 14 1559
挽巷
挽巷 2020-11-30 17:25

I have a tableview, where sometimes there might not be any results to list, so I would like to put something up that says \"no results\" if the

相关标签:
14条回答
  • 2020-11-30 18:08

    You can try this control. Its is pretty neat. DZNEmptyDataSet

    Or if I were you all I would do is

    • Check to see if your data array is empty
    • If it is empty then add one object called @"No Data" to it
    • Display that string in cell.textLabel.text

    Easy peasy

    0 讨论(0)
  • 2020-11-30 18:08

    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;
            }
    
    0 讨论(0)
提交回复
热议问题