Xcode 9 - iOS 11 UITableView行为空

8
自从新的iOS 11更新后,我有一个应用程序在模拟器和设备上显示空白的tableview行。即使是应该存在的所有行的行分隔符也不会显示。如果我将模拟器更改为旧的iOS版本,则行将正常显示。没有对代码或故事板进行任何更改。
这些行仍然具有数据,这意味着我可以点击其中一个空白行,并且它将执行代码并包含我期望的信息。
似乎我放置在视图上并且不使用内置文本标签的其他场景都很好。只有这个使用内置文本标签的tableview类出了问题。
以下是我的tableview类代码...
@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


1
我在一个项目上遇到了类似的问题,该项目在ios10上完美运行(现在所有单元格都是空白的。只有组标题是正常的)。我正在研究它。 - ZDidier
这里也有同样的问题。一定是iOS 11的一个bug。我通过在viewDidAppear()中调用tableView.reloadData()来解决它。虽然会有一点闪烁,但总比不出现好。另外,我注意到当重复使用单元格时(即将它们滚动到屏幕外再滚回来),它们会重新出现。 - marcshilling
1
TableView的ReloadData对我也没有用。我已经在我的viewWillAppear中尝试过了,还尝试添加了一个viewDidAppear,但都没有成功... - Kryckter
1
你找到解决方案了吗?我也遇到了同样的问题。 - MRK
@Kryckter,你找到解决这个问题的方法了吗? - torap
显示剩余5条评论
4个回答

3
这可能是因为你的tableView处于bgLayer之下。
问题似乎出在self.view.layer insertSublayer:bgLayer atIndex:0。我曾经使用insertSubview,并且遇到了同样的问题。这可能是iOS 11中的一个bug——如果你在storyboard中定义了tableView,它总是被推到后面。
最好的解决方法是把bgLayer放在tableView.backgroundView中。
注意:你也可以在viewDidAppear中调用sendSubviewToBack来解决它,但很不幸,tableview单元格每次reloaddata时都会移到后面,所以这不是一个好的解决方案。

干得好。谢谢:我移除了我的backgroundView [self.tableView insertSubview:backgroundView atIndex:0]; 现在它按预期工作。 - ZDidier

3

我也遇到过这个问题,使用IOS 11会出现空白单元格,但在IOS 10上却没问题。我的问题是使用了渐变色,不知道为什么导致单元格文本无法显示。删除渐变色后就解决了空白单元格的问题。


0
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];

为表格视图的背景视图设置渐变层对我解决了问题。在 iOS 10 及以下版本中,我们可以直接将子层插入到 tableView 中,但在 iOS 11 中,层应该仅插入到 UITableVIew 的背景视图中。


0
与上述的tableview子视图操作相配合,如果您的项目在Xcode 9之前存在,并且您在storyboard上勾选了使用安全区域布局指南,则也可能出现此问题。尝试取消勾选并查看是否有效。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接