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

后端 未结 14 1562
挽巷
挽巷 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 17:44

    I would present a an overlay view that has the look and message you want if the tableview has no results. You could do it in ViewDidAppear, so you have the results before showing/not showing the view.

    0 讨论(0)
  • 2020-11-30 17:49

    Use this code in Your numberOfSectionsInTableView method:-

    if ([array count]==0
    {
    
        UILabel *fromLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, self.view.frame.size.height/2, 300, 60)];                                                                                        
        fromLabel.text =@"No Result";
        fromLabel.baselineAdjustment = UIBaselineAdjustmentAlignBaselines;
        fromLabel.backgroundColor = [UIColor clearColor];
        fromLabel.textColor = [UIColor lightGrayColor];
        fromLabel.textAlignment = NSTextAlignmentLeft;
        [fromLabel setFont:[UIFont fontWithName:Embrima size:30.0f]];
        [self.view addSubview:fromLabel];
        [self.tblView setHidden:YES];
    }
    
    0 讨论(0)
  • 2020-11-30 17:50

    Swift 3 (updated):

    override func numberOfSections(in tableView: UITableView) -> Int {
        if myArray.count > 0 {
            self.tableView.backgroundView = nil
            self.tableView.separatorStyle = .singleLine
            return 1
        }
    
        let rect = CGRect(x: 0,
                          y: 0,
                          width: self.tableView.bounds.size.width,
                          height: self.tableView.bounds.size.height)
        let noDataLabel: UILabel = UILabel(frame: rect)
    
        noDataLabel.text = "Custom message."
        noDataLabel.textColor = UIColor.white
        noDataLabel.textAlignment = NSTextAlignment.center
        self.tableView.backgroundView = noDataLabel
        self.tableView.separatorStyle = .none
    
        return 0
    }
    
    0 讨论(0)
  • 2020-11-30 17:54

    If you don't use the tableview footer and do not want the tableview to fill up the screen with empty default table cells i would suggest that you set your tableview footer to an empty UIView. I do not know the correct syntax for doing this in obj-c or Swift, but in Xamarin.iOS i would do it like this:

    public class ViewController : UIViewController
    {
        UITableView _table;
    
        public ViewController (IntPtr handle) : base (handle)
        {
        }
    
        public override void ViewWillAppear(bool animated) {
            // Initialize table
    
            _table.TableFooterView = new UIView();
        }
    }
    

    Above code will result in a tableview without the empty cells

    0 讨论(0)
  • 2020-11-30 17:58

    Here is the solution that worked for me.

    1. Add the following code to a new file.

    2. Change your table class to the custom class "MyTableView" from storyboard or .xib

    (this will work for the first section only. If you want to customize more, do changes in the MyTableView reloadData() function accordingly for other sections)

    public class MyTableView: UITableView {
    
        override public func reloadData() {
            super.reloadData()
    
            if self.numberOfRows(inSection: 0) == 0 {
                if self.viewWithTag(1111) == nil {
                    let noDataLabel = UILabel()
                    noDataLabel.textAlignment = .center
                    noDataLabel.text = "No Data Available"
                    noDataLabel.tag = 1111
                    noDataLabel.center = self.center
                    self.backgroundView = noDataLabel
                }
    
            } else {
                if self.viewWithTag(1111) != nil {
                    self.backgroundView = nil
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-30 17:59

    SWIFT 3

            let noDataLabel: UILabel     = UILabel(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: tableView.bounds.size.height))
            noDataLabel.text             = "No data available"
            noDataLabel.textColor        = UIColor.white
            noDataLabel.font             = UIFont(name: "Open Sans", size: 15)
            noDataLabel.textAlignment    = .center
            tableView.backgroundView = noDataLabel
            tableView.separatorStyle = .none
    
    0 讨论(0)
提交回复
热议问题