ios - 在 iOS 7 上选择后 UITableViewCell 不更新

标签 ios objective-c iphone uitableview

所以我遇到了一个非常奇怪的问题。我的代码在 iOS 8 上运行良好,但在 iOS 7 上运行不佳,我不明白为什么。

我有一个 tableview,它有一个项目列表,当你选择一个项目时,一个复选标记被添加到该项目,如果你再次选择它,复选标记被删除。很简单吧? :-p 正如我所说,它在 iOS 8 上运行良好,但当我针对 iOS 7.1 运行时,单元格突出显示、添加复选标记并删除旧标题并将其替换为默认的 Title。之后,无论我点击单元格多少次,它都不会改变(但底层数据确实会改变)。

选择前 Before Selection

选择后 After Selection

如果我离开屏幕再返回,行会正确显示。我已验证正在调用 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/

相关文章:

ios - 安装cocoapod时生成的xcscheme。被忽视安全吗?

objective-c - if 条件如何与多个表达式一起使用

iphone - 在团队环境中合并时使用 Interface Builder (XIB) 还是代码?

iphone - 如何将本地 HTML 文件加载到 UIWebView 中?

ios - Xamarin.Forms ContentPage.BackgroundImage

c# - Xamarin 可移植类库中的简单获取请求(我需要它在 iOS 和 Windows Phone 上运行)

ios - 按下按钮时应用程序崩溃; iOS模拟

ios - 如何在标签中写多行

iphone - 我可以阻止 iPhone OS 3.x 应用程序在 2.x 操作系统上运行吗?

ios - NSPredicate 检查以逗号分隔的一串数字是否包含数字