UITwitch里面的UITwitch

时间:2012-06-20 21:30:32

标签: ios uitableview uiswitch

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

我把我的单元格设置为:

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”。

有什么问题?

1 个答案:

答案 0 :(得分:4)

一些即时反应:

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

  2. 我认为您希望将所有收藏夹保存为用户默认值。您当前的(IBAction)favorite:(id)sender每次都会重新创建favoriteList,并且其中包含一个项目或其中没有项目,但肯定会丢弃您过去可能选择的任何其他收藏。

  3. 一个小问题,但是如果您设置附件单元格,则无需将switchView添加到子视图中。

  4. 所以,我建议修改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];
    }
    

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