2

我有一个带有自定义单元格的表格视图(UISwitch在表格视图的每个单元格上)。

我已经这样设置了我的单元格:

switchView = [[UISwitch alloc] initWithFrame:CGRectMake(200.0f, 5.0f, 75.0f, 30.0f)];
cell.accessoryView = switchView;
[switchView setOn:NO animated:NO];
[switchView addTarget:self action:@selector(favorite:) forControlEvents:UIControlEventValueChanged];
[cell addSubview:switchView];

用户更改时调用的操作UISwitch是:

-(IBAction)favorite:(id)sender 
{
    indexPath = [self.tableView indexPathForCell:(UITableViewCell*)[sender superview]];
    NSMutableArray *favoriteList = [[NSMutableArray alloc]init];
    NSString *favoriteItem = [self.tableView cellForRowAtIndexPath:indexPath].textLabel.text;
    NSLog(favoriteItem);

    if ([switchView isOn]) 
    {
        [favoriteList addObject:favoriteItem];

        NSUserDefaults *favoriteDefaults = [NSUserDefaults standardUserDefaults];
        [favoriteDefaults setObject:favoriteList forKey:@"MyFavorites"];
        NSLog(@"%@", favoriteList);
    }
    else
    {
        [favoriteList removeObject:favoriteItem];

        NSUserDefaults *favoriteDefaults = [NSUserDefaults standardUserDefaults];
        [favoriteDefaults setObject:favoriteList forKey:@"MyFavorites"];
        NSLog(@"%@", favoriteList);
    }
}

问题是:
在测试应用程序时,只有表格视图的最后一项(单元格)可以正常工作。

对于其他人,调试器返回状态UISwitch始终为“OFF”。

问题是什么?

4

1 回答 1

4

一些直接的反应:

  1. (IBAction)favorite:(id)sender您没有获取相关单元格的 switchView 时,而只是使用该类 ivar 的最后一个引用所使用的内容。同样,在您的 中cellForRowAtIndexPath,您似乎正在设置(并反复重置)这个相同的单个switchViewivar。可能根本不应该是 ivar。您可以创建switchView一个局部变量cellForRowAtIndexPath. 您也可以将其设为 的局部变量favorite。但最重要的是,favorite应该将其设置为UISwitch *switchView = sender.

  2. 我假设您想将所有收藏夹保存到用户默认值。你的 current(IBAction)favorite:(id)sender每次都会重新创建 favoriteList,它要么有一个项目,要么没有项目,但肯定会丢弃你过去可能选择的任何其他收藏夹。

  3. 一个小问题,但是如果你设置了附属单元格,你不需要将 switchView 也添加到子视图中。

所以,我建议修改cellForRowAtIndexPath如下:

NSString *title = [_favorites objectAtIndex:indexPath.row]; // clearly, you'll get the text however you're getting your text now ... I'm just storing in array
cell.textLabel.text = title;

BOOL isFav = [self isFavorite:title]; // go to user defaults and find out if it's there or not

UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectMake(200.0f, 5.0f, 75.0f, 30.0f)];
[switchView setOn:isFav animated:NO];
[switchView addTarget:self action:@selector(toggleFavoriteSwitch:) forControlEvents:UIControlEventValueChanged];
cell.accessoryView = switchView;

这使用isFavorite

- (BOOL)isFavorite:(NSString *)title
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSArray *listOfFavorites = [userDefaults objectForKey:kMyFavsKey];

    if (listOfFavorites)
        return [listOfFavorites containsObject:title];

    return NO;
}

然后,显然,您需要定义您的toggleFavoriteSwitch(以前称为favorite)方法:

- (IBAction)toggleFavoriteSwitch:(UISwitch *)switchView
{
    UITableViewCell *cell = (UITableViewCell *)[switchView superview];

    [self updateFavoriteInUserDefaultsFor:cell.textLabel.text withValue:[switchView isOn]];
}

这正在更新用户默认值,如下所示:

- (void)updateFavoriteInUserDefaultsFor:(NSString *)title withValue:(BOOL)isOn
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

    NSArray *oldFavorites = [userDefaults objectForKey:kMyFavsKey];

    NSMutableArray *newFavorites;

    if (!oldFavorites)
        newFavorites = [[NSMutableArray alloc] init];
    else
        newFavorites = [NSMutableArray arrayWithArray:oldFavorites];

    if (isOn)
        [newFavorites addObject:title];
    else
        [newFavorites removeObject:title];

    [userDefaults setObject:newFavorites forKey:kMyFavsKey];

    [userDefaults synchronize];
}

希望这可以解决您的问题。

于 2012-06-22T22:30:09.790 回答