所以我遇到了一个非常奇怪的问题。我的代码在 iOS 8 上运行良好,但在 iOS 7 上运行不佳,我不明白为什么。
我有一个 tableview,它有一个项目列表,当你选择一个项目时,一个复选标记被添加到该项目,如果你再次选择它,复选标记被删除。很简单吧? :-p 正如我所说,它在 iOS 8 上运行良好,但当我针对 iOS 7.1 运行时,单元格突出显示、添加复选标记并删除旧标题并将其替换为默认的 Title。之后,无论我点击单元格多少次,它都不会改变(但底层数据确实会改变)。
选择前
选择后
如果我离开屏幕再返回,行会正确显示。我已验证正在调用 cellForRowAtIndexPath 并将正确的值添加到单元格中。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [[parkFinderSingleton.data valueForKey:@"amenities"] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"amenityFilterCell" forIndexPath:indexPath];
Amenity *currentAmenity;
NSArray *amenities = [parkFinderSingleton.data valueForKey:@"amenities"];
if (amenities != nil) {
currentAmenity = amenities[indexPath.row];
cell.textLabel.text = currentAmenity.amenityTitle;
NSLog(@"Cell Title %@", cell.textLabel.text);
if (currentAmenity.amenitySelected) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"amenityFilterCell" forIndexPath:indexPath];
Amenity *currentAmenity;
NSArray *amenities = [parkFinderSingleton.data valueForKey:@"amenities"];
if (amenities != nil) {
currentAmenity = amenities[indexPath.row];
if (currentAmenity.amenitySelected) {
currentAmenity.amenitySelected = NO;
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
currentAmenity.amenitySelected = YES;
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
}
对可能发生的事情有什么想法吗?
最佳答案
通常,dequeueReusableCellWithIdentifier:forIndexPath:
不应在 tableView: didSelectRowAtIndexPath:
中调用。如果您想要特定 indexPath 的单元格,请使用 [tableView cellForRowAtIndexPath:indexPath]
。
对于:
[self.tableView beginUpdates]; [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone]; [self.tableView endUpdates];
如果您只想取消选择一个单元格,请使用 [tableView deselectRowAtIndexPath:animated:]
。
关于ios - 在 iOS 7 上选择后 UITableViewCell 不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26435784/