UISwitch没有更新因为reloaddata

时间:2014-07-15 23:19:37

标签: ios objective-c

我通常喜欢自己解决问题,但我遇到了问题。现在我试图复制默认的IOS时钟应用程序,他们有一个带有uiswitch的tableviewcell。 现在我有一个@property (nonatomic, assign) BOOL alarmOn;跟踪开关,我不确定如何跟踪我有多个开关我知道我必须使用数组或NSNumber但我不太确定。现在的问题是当我创建一个显示在单元格上的时钟时,我切换开关。然后,如果我想在时钟上编辑时间,我在单元格内部单击并返回到tableviewcontroller,UISwitch被关闭。当我再次reload数据时我知道问题是viewillload但是我不知道解决这个问题的方法。我必须reload数据,因为当我在时钟内编辑时间时,我想更新tableview以显示正确的时间。

现在

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];

    // Create a switch button on each cell
    UISwitch *switchView = [[UISwitch alloc] init];
    //[switchView setOn:NO animated:YES];
    [switchView addTarget:self action:@selector(updateSwitch:) forControlEvents:UIControlEventValueChanged];
    [switchView setOnTintColor:COLOR_RGB(0, 174, 239, 1.0)];
    self.alarmOn = (switchView.on) ? YES : NO;

    cell.accessoryView = switchView;

    return cell;
}

UpdateSwitch

 - (void)updateSwitch:(id)sender
{
    UISwitch *switcher = (UISwitch *)sender;
    if (switcher.on)
    {
        [switcher setOn: YES animated:YES];
        self.alarmOn = YES;
        NSLog(@"ON");
    }
    else
    {
        [switcher setOn: NO animated: YES];
        self.alarmOn = NO;
        NSLog(@"OFF");
    }

}

ViewWillLoad

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self.tableView reloadData];
}

1 个答案:

答案 0 :(得分:0)

您只需将开关值存储在由NSMutableDictionar键入的indexPath中,然后再将其恢复。如下:

使用此类别查找UISwitch的parentTableViewCell:

@implementation UIView (ParentTableViewCell category)

- (UITableViewCell *)parentTableViewCell
{
    // iterate up the view hierarchy to find the table cell containing this view
    UIView *aView = self.superview;
    while(aView != nil) {
        if([aView isKindOfClass:[UITableViewCell class]]) {
            return (UITableViewCell *)aView;
        }
        aView = aView.superview;
    }
    return nil; // this view is not within a tableView cell
}

@end

然后:

- (void)viewWillLoad:(BOOL)animated
{
    [super viewWillLoad:animated];

    self.switchValues = [MSMutableDictionary new];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self.tableView reloadData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];

    // Create a switch button on each cell
    UISwitch *switchView = [[UISwitch alloc] init];

    // Get previous switchValue. Will be NO if no previous value
    BOOL switchValue = [self.switchValues[indexPath] boolValue];
    [switchView setOn:switchValue animated:YES];

    [switchView addTarget:self action:@selector(updateSwitch:) forControlEvents:UIControlEventValueChanged];
    [switchView setOnTintColor:COLOR_RGB(0, 174, 239, 1.0)];
    self.alarmOn = (switchView.on) ? YES : NO;

    cell.accessoryView = switchView;

    return cell;
}

- (void)updateSwitch:(id)sender
{
    UISwitch *switcher = (UISwitch *)sender;
    UITableViewCell *cell = [switcher parentTableViewCell];
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

    // Store the value as an NSNumber keyed by indexPath
    self.switchValues[indexPath] = @( switcher.on );

    if (switcher.on)
    {
        [switcher setOn: YES animated:YES];
        self.alarmOn = YES;
        NSLog(@"ON");
    }
    else
    {
        [switcher setOn: NO animated: YES];
        self.alarmOn = NO;
        NSLog(@"OFF");
    }
}