UITableView 通过上下滚动来选择单元格

发布于 2024-11-28 03:45:38 字数 3090 浏览 2 评论 0原文

我的 iPhone 应用程序中有一个分组表视图,其中包含一天中所有 24 小时的列表。当用户选择表视图中的某个单元格时,它会在其附件视图中显示一个复选标记,并通过 NSUserDefaults 保存数据。当用户继续加载视图时,这意味着我必须从 NSUserDefaults 中提取信息并在相应的单元格上显示复选标记。在我开始在视图中滚动之前,这一切都很好。然后会发生这种情况:

在此处输入图像描述

我已经设置了我的 didSelectRowAtIndexPath ,这样当行被选中,所有其他行都被取消选择。这工作得很好,但只需滚动列表,看似随机的项目就会开始被选择。如果我继续上下滚动几次,所有项目都将被选中。如果我单击其中一个单元格,则会选择该单元格,而取消选择其他单元格(这是应该的方式)。

但是为什么只通过滚动就可以选择单元格呢?这是我的 cellForRowAtIndexPath 方法,问题可能出在:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    NSString *cellValue;

    if (indexPath.row == 0) {
        cellValue = @"00:00";
    } else if (indexPath.row == 1) {
        cellValue = @"01:00";
    } else if (indexPath.row == 2) {
        cellValue = @"02:00";
    } else if (indexPath.row == 3) {
        cellValue = @"03:00";
    } else if (indexPath.row == 4) {
        cellValue = @"04:00";
    } else if (indexPath.row == 5) {
        cellValue = @"05:00";
    } else if (indexPath.row == 6) {
        cellValue = @"06:00";
    } else if (indexPath.row == 7) {
        cellValue = @"07:00";
    } else if (indexPath.row == 8) {
        cellValue = @"08:00";
    } else if (indexPath.row == 9) {
        cellValue = @"09:00";
    } else if (indexPath.row == 10) {
        cellValue = @"10:00";
    } else if (indexPath.row == 11) {
        cellValue = @"11:00";
    } else if (indexPath.row == 12) {
        cellValue = @"12:00";
    } else if (indexPath.row == 13) {
        cellValue = @"13:00";
    } else if (indexPath.row == 14) {
        cellValue = @"14:00";
    } else if (indexPath.row == 15) {
        cellValue = @"15:00";
    } else if (indexPath.row == 16) {
        cellValue = @"16:00";
    } else if (indexPath.row == 17) {
        cellValue = @"17:00";
    } else if (indexPath.row == 18) {
        cellValue = @"18:00";
    } else if (indexPath.row == 19) {
        cellValue = @"19:00";
    } else if (indexPath.row == 20) {
        cellValue = @"20:00";
    } else if (indexPath.row == 21) {
        cellValue = @"21:00";
    } else if (indexPath.row == 22) {
        cellValue = @"22:00";
    } else if (indexPath.row == 23) {
        cellValue = @"23:00";
    }

    if ([[prefs valueForKey:@"quiet_hours_time_interval_from"] isEqualToString:cellValue]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        cell.textLabel.textColor = [UIColor colorWithRed:72.0 / 255 green:104.0 / 255 blue:152.0 / 255 alpha:1];
    }

    cell.textLabel.text = cellValue;

    return cell;
}

prefs 等于 [NSUserDefaults standardUserDefaults] 并且我保存了(正确)将选定的单元格添加到 quiet_hours_time_interval_from 键。

希望大家能帮忙。

I have a grouped table view in my iPhone application that contains a list of all 24 hours of the day. When the user selects one of the cells in the table view, it shows a checkmark in its accessory view and saves the data via NSUserDefaults. When the user loads the view going forward, this means that I have to pull the information out of the NSUserDefaults and display a checkmark on the corresponding cell. This works fine until I start scrolling in the view. Then this happens:

enter image description here

I've set my didSelectRowAtIndexPath up such that when one row is selected, all the others are deselected. This works just fine, but by just scrolling through the list, seemingly random items start being selected. If I continue to scroll up and down a few times, all items will have been selected. If I click on one of the cells, that one cell is selected while the others' are deselected (which is the way it should be).

But how come the cells are selected just by scrolling? Here is my cellForRowAtIndexPath method, where the problem probably is located:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    NSString *cellValue;

    if (indexPath.row == 0) {
        cellValue = @"00:00";
    } else if (indexPath.row == 1) {
        cellValue = @"01:00";
    } else if (indexPath.row == 2) {
        cellValue = @"02:00";
    } else if (indexPath.row == 3) {
        cellValue = @"03:00";
    } else if (indexPath.row == 4) {
        cellValue = @"04:00";
    } else if (indexPath.row == 5) {
        cellValue = @"05:00";
    } else if (indexPath.row == 6) {
        cellValue = @"06:00";
    } else if (indexPath.row == 7) {
        cellValue = @"07:00";
    } else if (indexPath.row == 8) {
        cellValue = @"08:00";
    } else if (indexPath.row == 9) {
        cellValue = @"09:00";
    } else if (indexPath.row == 10) {
        cellValue = @"10:00";
    } else if (indexPath.row == 11) {
        cellValue = @"11:00";
    } else if (indexPath.row == 12) {
        cellValue = @"12:00";
    } else if (indexPath.row == 13) {
        cellValue = @"13:00";
    } else if (indexPath.row == 14) {
        cellValue = @"14:00";
    } else if (indexPath.row == 15) {
        cellValue = @"15:00";
    } else if (indexPath.row == 16) {
        cellValue = @"16:00";
    } else if (indexPath.row == 17) {
        cellValue = @"17:00";
    } else if (indexPath.row == 18) {
        cellValue = @"18:00";
    } else if (indexPath.row == 19) {
        cellValue = @"19:00";
    } else if (indexPath.row == 20) {
        cellValue = @"20:00";
    } else if (indexPath.row == 21) {
        cellValue = @"21:00";
    } else if (indexPath.row == 22) {
        cellValue = @"22:00";
    } else if (indexPath.row == 23) {
        cellValue = @"23:00";
    }

    if ([[prefs valueForKey:@"quiet_hours_time_interval_from"] isEqualToString:cellValue]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        cell.textLabel.textColor = [UIColor colorWithRed:72.0 / 255 green:104.0 / 255 blue:152.0 / 255 alpha:1];
    }

    cell.textLabel.text = cellValue;

    return cell;
}

prefs is equal to [NSUserDefaults standardUserDefaults] and I save the exact value of the (properly) selected cell to the quiet_hours_time_interval_from key.

Hope you guys can help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事灯 2024-12-05 03:45:38

在检查首选项 valueForKey 后,您需要一个 ELSE。您需要清除 ELSE 中的accessoryType。

您所看到的是 UITableView 的默认行为。单元会被缓存,并且仅在需要时重新创建。这就是顶部的位对 dequeueReusableCell 所做的事情。

因此,由于您没有任何清除accessoryType的代码,因此最终所有这些都会被检查。

You need an ELSE after your check for prefs valueForKey. You need to clear the accessoryType in that ELSE.

What you are seeing is the default behavior of the UITableView. Cells are cached, and only recreated when needed. That's what the bit at the top does with dequeueReusableCell.

So, since you don't have any code that ever clears the accessoryType, eventually all of them end up checked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文