3

In a table view, i am showing "No results found" if no results returned by search controller by adding an else condition as below-

In numberOfRowsInSection:

else if self.searchController.active && self.searchController.searchBar.text?.characters.count > 0 &&
        self.filteredLanguages.count == 0 {
        return 1
}

In cellForRowAtIndexPath:

else if self.searchController.active && self.searchController.searchBar.text?.characters.count > 0 &&
        self.filteredLanguages.count == 0 {
        cell.textLabel?.text = "No results found"
        cellImage = nil
        cell.userInteractionEnabled = false
        isNoResultCell = true
}

if filteredIndexPath!.row == userSettings.valueForKey("selectedRow") as? Int  && !isNoResultCell {
        cell.tintColor = UIColor.whiteColor()
        cell.accessoryType = UITableViewCellAccessoryType.Checkmark
        lastSelectedRow = filteredIndexPath
}

I did some research about approaches to show "No results found" in table view but that after writing above logic.

And as per few posts, i can add a UILabel as sub view in background view of table in numberOfSectionsInTableView when there are no search results and remove the subview if search results are returned.

My question is - Which is the most resource/performance savvy option ?

CuriousDev
  • 301
  • 3
  • 16
  • 1
    i think you should have a hidden label in your view and show when you didn't found any thing that time hide table – Prashant Tukadiya Apr 06 '16 at 08:17
  • 1
    There are many way to do this without using subview. Using section, tableHeaderView, sectionHeaderView and etc. Their performance are almost same. – Satachito Apr 06 '16 at 08:20
  • @PKT - Thanks !! But can you suggest if the logic i had written (as above, it solves the purpose) is fine with performance/resource usage perspective ? – CuriousDev Apr 08 '16 at 18:14
  • @Satachito - will you please suggest if the logic i had written (as above, it solves the purpose) is fine with performance/resource usage perspective ? – CuriousDev Apr 08 '16 at 18:15

4 Answers4

3

try this

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSInteger numOfSections = 0;
    if (youHaveData)
    {
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        numOfSections                 = 1;
       yourTableView.backgroundView   = nil;
    }
    else
    {   
        UILabel *noDataLabel         = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, yourTableView.bounds.size.width, yourTableView.bounds.size.height)];
        noDataLabel.text             = @"No data available";
        noDataLabel.textColor        = [UIColor blackColor];
        noDataLabel.textAlignment    = NSTextAlignmentCenter;
        yourTableView.backgroundView = noDataLabel;
        yourTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }

    return numOfSections;
}
Jigar
  • 1,801
  • 1
  • 14
  • 29
  • It works but setting tableView.backgroundView = nil or label removes the blur view i am setting for tableView background. – CuriousDev Apr 08 '16 at 18:30
3

You need to take a UILabel that will be hidden when your tableview have records (i.e. array having records that is going to be shown in tableview).

When you search a record and you get no records the you can hide tableview and show label having text "No results found".

Mahendra
  • 8,448
  • 3
  • 33
  • 56
  • Thanks ! this one is the easiest of all. I did it with adding an UILabel object from object Inspector to storyboard & creating an IBOutlet connection. The problem is, i cannot place in the centre of tableview using constraints. It automatically forces to place it below prototype cell. – CuriousDev Apr 08 '16 at 18:10
  • Add label in main view and add centerX, centerY, height and width constraints for it...by default be it in hidden...when user searches a record and if that is not found then just show label else be it hidden..that's it. – Mahendra Apr 08 '16 at 18:20
  • do you mean placing the label within tableview above prototype cell and add constraints or set label frame position programmatically? I still can't see any option to align it in storyboard/ attribute inspector. Sorry, I am new to iOS dev , so quite oblivious of doing this. – CuriousDev Apr 08 '16 at 19:04
  • No No not in tableview. Its in view, main view on which you have put tableview. Just put label and set constraints. – Mahendra Apr 08 '16 at 19:11
  • Actually main view itself a UITableViewController. So, when i drag and drop label it only allows to drop within tableview, cell or contentview – CuriousDev Apr 08 '16 at 19:31
  • This is the simplest solution. However, with `UITableViewController` this is tricky. It's easier to use a header or a footer to display the text and disable scrolling on the table view. – Sulthan Mar 31 '17 at 14:17
0

I can surely recommend you this great library, https://github.com/dzenbot/DZNEmptyDataSet it will be work on behalf on you. And you can easily set a place holder either normal string or a attributed string or an image in place of UICollectionView or UITableView.

When you will have the data just reload your object and it will hide that placeholder thing.

Use:

In viewDidLoad:

self.tableView.emptyDataSetSource = self;
self.tableView.emptyDataSetDelegate = self;

Implement following delegate:

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString *text = @"No results found.";

    NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:18.0f], NSForegroundColorAttributeName: [UIColor darkGrayColor]};

    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

For more info and usage, please check out above link.

Oh yes, to use it inside Swift project: How to call Objective-C code from Swift.

Community
  • 1
  • 1
Hemang
  • 26,840
  • 19
  • 119
  • 186
  • Thanks !! I have to give it a try. First of all, i am trying out easiest solutions. This would really helpful for my other apps. The current app in discussion is a simpler one. – CuriousDev Apr 08 '16 at 18:32
0

Place a UILabel behind the table. When the table count is 0 then make the table "isHidden" = true. This will hide the table and you will then see the layer underneath it. The UI label will be shown since it is directly beneath the tableview.

Daniel
  • 2,028
  • 20
  • 18