当核心数据状态改变时,UISwitch不会打开

发布于 2024-11-29 18:11:25 字数 2298 浏览 2 评论 0原文

我有一个表,其中每一行代表核心数据中的一个对象。每行都包含一个 UISwitch(修改为包含包含它的行的索引路径),切换时应切换核心数据中对象的状态属性。

目前发生的情况是,触摸开关时开关会变为打开状态,然后又返回关闭状态!?

我得到的唯一线索是,当我注释掉

// Establish what position the switch should be in
    if([info.state isEqualToString:@"on"]){
        mySwitch.selected = TRUE;
    } else {
        mySwitch.selected = FALSE;
    }

以及changeState 方法中的两行info.state 行时,开关工作正常。看起来 info.state 设置器,无论它们对开关状态的影响如何,都会阻止开关切换。

我是否只是非常疏忽并且没有正确管理内存?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...
    [self configureCell:cell atIndexPath:indexPath];
    return cell;
}

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    InfoObject *info = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = info.name;

    // Use a custom UISwitch that holds a pointer to the row it is associated with
    NamedUISwitch *mySwitch = [[[NamedUISwitch alloc] initWithFrame:CGRectZero] autorelease];
    mySwitch.indexPath = indexPath;
    [mySwitch addTarget:self action:@selector(changeState:) forControlEvents:UIControlEventValueChanged];
    // And of course we need to know what the state is
    NSLog(@"switchState:%@",info.state);

    // Establish what position the switch should be in
    if([info.state isEqualToString:@"on"]){
        mySwitch.selected = TRUE;
    } else {
        mySwitch.selected = FALSE;
    }

    cell.accessoryView = mySwitch;
}

- (void) changeState:(id)sender {
    InfoObject *info = [self.fetchedResultsController objectAtIndexPath:((NamedUISwitch*)sender).indexPath];

    if(((NamedUISwitch*)sender).on){
        info.state = @"on";
    } else {
        info.state = @"off";
    }

    NSError *error;
    if (![self.context save:&error]) {
        NSLog(@"Whoops, couldn't save state: %@", [error localizedDescription]);
        //TODO: alertview
    }
}

I have a table where each row represents an object in core data. Each row contains a UISwitch (modified to contain the indexPath of the row that contains it) that when toggled should toggle the state property of the object in core data.

What's currently happening is the switch goes to on when it is touched, and then goes back to off!?

The one clue I have is that when I comment out

// Establish what position the switch should be in
    if([info.state isEqualToString:@"on"]){
        mySwitch.selected = TRUE;
    } else {
        mySwitch.selected = FALSE;
    }

and the two info.state lines in the changeState method, the switch works fine. It seems like the info.state setter, regardless of their bearing on the switch state, prevent the switch from switching.

Am I just being colossally negligent and not managing memory correctly?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...
    [self configureCell:cell atIndexPath:indexPath];
    return cell;
}

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    InfoObject *info = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = info.name;

    // Use a custom UISwitch that holds a pointer to the row it is associated with
    NamedUISwitch *mySwitch = [[[NamedUISwitch alloc] initWithFrame:CGRectZero] autorelease];
    mySwitch.indexPath = indexPath;
    [mySwitch addTarget:self action:@selector(changeState:) forControlEvents:UIControlEventValueChanged];
    // And of course we need to know what the state is
    NSLog(@"switchState:%@",info.state);

    // Establish what position the switch should be in
    if([info.state isEqualToString:@"on"]){
        mySwitch.selected = TRUE;
    } else {
        mySwitch.selected = FALSE;
    }

    cell.accessoryView = mySwitch;
}

- (void) changeState:(id)sender {
    InfoObject *info = [self.fetchedResultsController objectAtIndexPath:((NamedUISwitch*)sender).indexPath];

    if(((NamedUISwitch*)sender).on){
        info.state = @"on";
    } else {
        info.state = @"off";
    }

    NSError *error;
    if (![self.context save:&error]) {
        NSLog(@"Whoops, couldn't save state: %@", [error localizedDescription]);
        //TODO: alertview
    }
}

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

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

发布评论

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

评论(1

天生の放荡 2024-12-06 18:11:25

我认为代码应该是:

// Establish what position the switch should be in
    if([info.state isEqualToString:@"on"]){
        mySwitch.on = YES;
    } else {
        mySwitch.on = NO;
    }

I think that code should be:

// Establish what position the switch should be in
    if([info.state isEqualToString:@"on"]){
        mySwitch.on = YES;
    } else {
        mySwitch.on = NO;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文