【问题标题】:Xcode13 iOS15 tableView cellForRowAtIndexPath: return a different cellXcode 13 iOS 15 tableView cellForRowAtIndexPath:返回不同的单元格
【发布时间】:2021-12-09 01:16:04
【问题描述】:

在iOS15的Xcode13中,每次调用[tableView reloadData];cellForRowAtIndexPath总是返回不同的cell,这让我的app很奇怪……所以我写了一个Demo来验证一下。

- (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];
}

当我在 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}

当我在 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}

有谁知道iOS15发生了什么变化以及如何解决?

【问题讨论】:

    标签: uitableview ios15


    【解决方案1】:

    您可以通过为每个单元格传递不同的标识符来解决此问题。结帐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"];
    

    【讨论】:

    • 谢谢!我看了视频,在iOS15.0@property (nonatomic, getter=isPrefetchingEnabled) BOOL prefetchingEnabled API_AVAILABLE(ios(15.0), tvos(15.0), watchos(8.0)); 找到了一个新属性,设置为No,我的应用运行良好
    猜你喜欢
    • 1970-01-01
    • 2011-10-09
    • 1970-01-01
    • 2013-09-24
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多