首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xcode13 iOS15 tableView cellForRowAtIndexPath:返回不同的单元格

Xcode13 iOS15 tableView cellForRowAtIndexPath:返回不同的单元格
EN

Stack Overflow用户
提问于 2021-10-22 11:29:40
回答 1查看 490关注 0票数 1

在Xcode13 iOS15中,每次我调用[tableView reloadData]; cellForRowAtIndexPath都会返回一个不同的单元格,这让我的应用程序非常奇怪……所以我写了一个Demo来验证这一点。

代码语言:javascript
复制
- (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中测试代码时

代码语言:javascript
复制
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中测试代码时

代码语言:javascript
复制
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上发生了什么变化以及如何解决吗?

EN

回答 1

Stack Overflow用户

发布于 2021-10-24 23:25:21

您可以通过为每个单元格传递不同的标识符来解决此问题。检出https://developer.apple.com/videos/play/wwdc2021/10252/

代码语言:javascript
复制
NSString *identifier = [NSString stringWithFormat:@"cell_identifer_%ld_%ld", indexPath.section, indexPath.row];

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifier"];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69676109

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档