Xcode 9 - iOS 11 UITableView rows are empty

Viewed 1557

Ever since the new iOS 11 update, I have an app that will show blank tableview rows on the simulator and device. Even the row separators will not show for any of the rows that are supposed to be there. If I change the simulator to an older iOS version, the rows will show fine. No changes to code or storyboard.

The rows still have the data, meaning I can tap on one of the blank rows and it will execute the code and contain the information I was expecting.

It appears that other scenes that I have where the tableview is placed on a view and I don't use the built in text label work fine. Just this tableview class using the built in text label.

Here is my code for the tableview class...

@interface BunkPickTableViewController ()

@end

@implementation BunkPickTableViewController

@synthesize appDelegate, delegate, bunkPassedValue;

- (void)viewDidLoad {

    [super viewDidLoad];

    UIView *backgroundView = [[UIView alloc] initWithFrame:self.view.bounds];

    appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    CAGradientLayer *bgLayer = [BackgroundLayer tanGradient];
    bgLayer.frame = self.view.bounds;
    [self.view.layer insertSublayer:bgLayer atIndex:0];

    self.tableView.backgroundView = backgroundView;

    [self.navigationController.navigationBar setTintColor:[UIColor blackColor]];

    self.title = @"Bunk Codes";

}

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    [[self navigationController] setNavigationBarHidden:NO animated:NO];

    [self.tableView reloadData];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [appDelegate.bunkArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        cell.textLabel.textAlignment = NSTextAlignmentCenter;

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        cell.backgroundColor = [UIColor clearColor];

    }

    Bunk *bunkObj = [appDelegate.bunkArray objectAtIndex:indexPath.row];

    cell.textLabel.text = bunkObj.bunkId;

    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;

    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];

    Bunk *bunkObj  = [appDelegate.bunkArray objectAtIndex:indexPath.row];

    [delegate dismissBunkPop:bunkObj.bunkId];

    [self.navigationController popViewControllerAnimated:YES];

}

@end

TableView Settings Image

4 Answers

I had this issue as well with IOS 11 showing blank cells.. but in IOS 10 was fine. My issue was that I was using a gradient which for whatever reason stopped the cell text from being shown. Removing the gradient resolved the blank cells.

This is probably because your tableView gets under bgLayer.

The problem seems to be in self.view.layer insertSublayer:bgLayer atIndex:0. I was using insertSubview at index 0 and I had the same problem. This is probably a bug in iOS 11 - if your tableView is defined in storyboard, it always gets pushed to back.

The best solution is to put the bgLayer inside the tableView.backgroundView.

NOTE: You could also solve it by calling sendSubviewToBack on the bgLayer in viewDidAppear, but unfortunetaly, tableview cells are moving to back on every tableview reloaddata, so it is not a good solution.

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.tableView.bounds;
gradient.colors = @[(id)[UIColor blackColor].CGColor, (id)[UIColor 
                    grayColor].CGColor, (id)[UIColor 
                    lightGrayColor].CGColor];

UIView *bgView = [[UIView alloc] 
                          initWithFrame:self.tableView.bounds];
[self setBackgroundView:bgView];
[bgView.layer insertSublayer:gradient atIndex:0];

[self.tableView setBackgroundView:bgView];

Setting gradient layer for tableview's background view solved the problem for me. Upto iOS 10 we can directly insert sublayer to tableView but in iOS 11 layer should be inserted to UITableVIew's background view only.

Related