【问题标题】:UISwitch inside of UITableViewCellUITableViewCell 内部的 UISwitch
【发布时间】:2012-06-20 21:30:31
【问题描述】:

我有一个带有自定义单元格的表格视图(表格视图的每个单元格上都有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”。

有什么问题?

【问题讨论】:

    标签: ios uitableview uiswitch


    【解决方案1】:

    几个即时反应:

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

    2. 我假设您想将所有收藏夹保存为用户默认设置。您当前的(IBAction)favorite:(id)sender 每次都会重新创建喜爱列表,其中要么有一个项目,要么没有项目,但肯定会丢弃您过去可能选择的任何其他收藏夹。

    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];
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-09
      • 2018-03-14
      相关资源
      最近更新 更多