在Xcode13 iOS15中,每次我调用[tableView reloadData]; cellForRowAtIndexPath都会返回一个不同的单元格,这让我的应用程序非常奇怪……所以我写了一个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上发生了什么变化以及如何解决吗?
发布于 2021-10-24 23:25:21
您可以通过为每个单元格传递不同的标识符来解决此问题。检出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"];https://stackoverflow.com/questions/69676109
复制相似问题