4

In Xcode13 iOS15, every time I call [tableView reloadData]; cellForRowAtIndexPath always return a different cell, it makes my app very strange... So I wrote a Demo to verify this.

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

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

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 300;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifier"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"identifier"];
    }
    if (indexPath.row & 1) {
        cell.backgroundColor = [UIColor blueColor];
    } else {
        cell.backgroundColor = [UIColor redColor];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%@ %@", [self.tableView cellForRowAtIndexPath:indexPath], indexPath);
    [self.tableView reloadData];
}

When I test my code in iOS15

2021-10-22 19:17:20.165792+0800 JQIApp[7636:1158455] <UITableViewCell: 0x7fa25770c670; frame = (0 0; 375 300); autoresize = W; layer = <CALayer: 0x6000032c0240>> <NSIndexPath: 0xe668878fbccf53cf> {length = 2, path = 0 - 0}
2021-10-22 19:17:20.343009+0800 JQIApp[7636:1158455] <UITableViewCell: 0x7fa25a009350; frame = (0 0; 375 300); autoresize = W; layer = <CALayer: 0x6000032fb380>> <NSIndexPath: 0xe668878fbccf53cf> {length = 2, path = 0 - 0}
2021-10-22 19:17:20.548892+0800 JQIApp[7636:1158455] <UITableViewCell: 0x7fa25a0053b0; frame = (0 0; 375 300); autoresize = W; layer = <CALayer: 0x6000032fb160>> <NSIndexPath: 0xe668878fbccf53cf> {length = 2, path = 0 - 0}
2021-10-22 19:17:20.712690+0800 JQIApp[7636:1158455] <UITableViewCell: 0x7fa25770c670; frame = (0 0; 375 300); autoresize = W; layer = <CALayer: 0x6000032c0240>> <NSIndexPath: 0xe668878fbccf53cf> {length = 2, path = 0 - 0}

When I test my code in iOS14

2021-10-22 19:18:43.014213+0800 JQIApp[8053:1170522] <UITableViewCell: 0x7f8a08817200; frame = (0 0; 375 300); autoresize = W; layer = <CALayer: 0x60000258d660>> <NSIndexPath: 0xe365b32ef41b1634> {length = 2, path = 0 - 0}
2021-10-22 19:18:43.409228+0800 JQIApp[8053:1170522] <UITableViewCell: 0x7f8a08817200; frame = (0 0; 375 300); autoresize = W; layer = <CALayer: 0x60000258d660>> <NSIndexPath: 0xe365b32ef41b1634> {length = 2, path = 0 - 0}
2021-10-22 19:18:43.834151+0800 JQIApp[8053:1170522] <UITableViewCell: 0x7f8a08817200; frame = (0 0; 375 300); autoresize = W; layer = <CALayer: 0x60000258d660>> <NSIndexPath: 0xe365b32ef41b1634> {length = 2, path = 0 - 0}
2021-10-22 19:18:44.410295+0800 JQIApp[8053:1170522] <UITableViewCell: 0x7f8a08817200; frame = (0 0; 375 300); autoresize = W; layer = <CALayer: 0x60000258d660>> <NSIndexPath: 0xe365b32ef41b1634> {length = 2, path = 0 - 0}

Does anyone know what has changed on iOS15 and how to resolve it?

黄嘉琦
  • 41
  • 2

1 Answers1

1

You can resolve this by passing different identifier for each cell. Checkout https://developer.apple.com/videos/play/wwdc2021/10252/

NSString *identifier = [NSString stringWithFormat:@"cell_identifer_%ld_%ld", indexPath.section, indexPath.row];

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifier"];
Saeed Nasehi
  • 940
  • 1
  • 11
  • 27
  • 6
    Thanks! I watch the video and find a new property in iOS15.0`@property (nonatomic, getter=isPrefetchingEnabled) BOOL prefetchingEnabled API_AVAILABLE(ios(15.0), tvos(15.0), watchos(8.0)); `,set it to No and my app run great – 黄嘉琦 Oct 25 '21 at 06:30