Show custom label when no data available for tableView

后端 未结 2 2148
误落风尘
误落风尘 2021-02-14 18:24

The app in question has an ability for users to mark items as favourite. When the user has no favourites saved, I\'d like to notify them of the fact (mostly I hate the idea of

相关标签:
2条回答
  • 2021-02-14 19:03

    What I personally would do is the following. As you said yourself, in the method numberOfRowsInSection you should check the count of your tableView datasource, if its 0 you should return 1 in order to display the 1 cell ou want.

    Then in cellForRowAtIndexPath you should check the count again, if it returns 0 you know that the table view is trying to draw your "You currently have no favorites" and you can set the text.

    0 讨论(0)
  • 2021-02-14 19:06

    1) In my own iOS apps, I create a subview with a UILabel in it, saying "No Results" When numberOfSectionsInTableView is zero, I add the subview to the tableview. When it is != zero, I remove it. This of course requires maintaining the subview in memory as a retained object, so that you can remove it.

    2) If you want to make one cell as you somewhat mentioned: When numberOfSectionsInTableView is zero, return 1. Then, in numberOfRowsInSection return 1 as well. In cellForRowAtIndexPath, check both numberOfSectionsInTableView and numberOfRowsInSection, and if they SHOULD be zero (after all you returned 1), then display whatever message.

    Solution 1 requires maintaining a variable to hold the subview, but is shorter and cleaner code, and I believe is less CPU intensive (not that either will cause any significant performance issues).

    Edit to include code: It was actually a little different than I thought, but basically the same. There are obviously things in here that are to be changed to fit your code and taste.

    (Make sure to declare UIView *nomatchesView; in .h)

    - (void)viewDidLoad{
    
            nomatchesView = [[UIView alloc] initWithFrame:self.view.frame];
            nomatchesView.backgroundColor = [UIColor clearColor];
    
            UILabel *matchesLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,320,320)];
            matchesLabel.font = [UIFont boldSystemFontOfSize:18];
            matchesLabel.minimumFontSize = 12.0f;
            matchesLabel.numberOfLines = 1;
            matchesLabel.lineBreakMode = UILineBreakModeWordWrap;
            matchesLabel.shadowColor = [UIColor lightTextColor];
            matchesLabel.textColor = [UIColor darkGrayColor];
            matchesLabel.shadowOffset = CGSizeMake(0, 1);
            matchesLabel.backgroundColor = [UIColor clearColor];
            matchesLabel.textAlignment =  UITextAlignmentCenter;
    
            //Here is the text for when there are no results
            matchesLabel.text = @"No Matches";
    
    
            nomatchesView.hidden = YES;
            [nomatchesView addSubview:matchesLabel];
            [self.tableView insertSubview:nomatchesView belowSubview:self.tableView];
        }
    
    
    
        - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
        {
            //If there is no table data, unhide the "No matches" view
            if([data count] == 0 ){
            nomatchesView.hidden = NO;
            } else {
            nomatchesView.hidden = YES;
            }
    
            return [data count];
        }
    
    0 讨论(0)
提交回复
热议问题