将开关设置为On,重新加载表然后插入一个新行

时间:2010-12-20 22:28:14

标签: iphone insert switch-statement tableview rows

我有一个UITableViewController,我用它来创建我的应用程序的设置。

有一个部分只有一行放置UISwitch 如果开关设置为YES,如何在开关的同一部分内插入一个新行?如果开关设置为NO,我怎么能删除这一行?

任何人都可以帮助我吗?谢谢!

我尝试使用insertRowsAtIndexPaths:withRowAnimation:方法但不起作用...
这是我的设置表代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = NSLocalizedString(@"Impostazioni", @"");
}


- (void)viewWillAppear:(BOOL)animated {
    [self.tableView reloadData];
}

-(void)addCellToSetCode:(id)sender {

    if ([codeSwitch isOn]) {

        NSIndexPath *updatedIndexPath = [NSIndexPath indexPathForRow:1 inSection:2];
        [self.tableView beginUpdates];
        [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:updatedIndexPath] withRowAnimation:UITableViewRowAnimationTop];
        [self.tableView endUpdates];

        [[NSUserDefaults standardUserDefaults] setBool:codeSwitch.on forKey:@"stateOfSwitch"];
    }
    else {

        NSIndexPath *updatedIndexPath = [NSIndexPath indexPathForRow:1 inSection:2];
        [self.tableView beginUpdates];
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:updatedIndexPath] withRowAnimation:UITableViewRowAnimationTop];
        [self.tableView endUpdates];

        [[NSUserDefaults standardUserDefaults] setBool:codeSwitch.on forKey:@"stateOfSwitch"];
    }
}


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return NSLocalizedString(@"ListaDesideri", @"");
    }
    if (section == 1) {
        return NSLocalizedString(@"CondivisioneMail", @"");
    }
    if (section == 2) {
        return @"Sicurezza";
    }
    else {
        return nil;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) || (section == 2) || (section == 3) {
        return 2;
    }
    else if (section == 1) {
        return 1;
    }
    else {
        return 1;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }
    if (indexPath.section == 0 && indexPath.row == 0) {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        cell.textLabel.text = NSLocalizedString(@"Ordine", @"");

        if ([[defaults objectForKey:@"ordinaPer"] isEqualToString:@"Nome"]) {
            cell.detailTextLabel.text = NSLocalizedString(@"Nome", @"");
        }
        if ([[defaults objectForKey:@"ordinaPer"] isEqualToString:@"Costo"]) {
            cell.detailTextLabel.text = NSLocalizedString(@"Costo", @"");
        }
        if ([[defaults objectForKey:@"ordinaPer"] isEqualToString:@"Categoria"]) {
            cell.detailTextLabel.text = NSLocalizedString(@"Categoria", @"");
        }
        if ([[defaults objectForKey:@"ordinaPer"] isEqualToString:@"Nome Discendente"]) {
            cell.detailTextLabel.text = NSLocalizedString(@"NomeDiscendente", @"");
        }
        if ([[defaults objectForKey:@"ordinaPer"] isEqualToString:@"Costo Discendente"]) {
            cell.detailTextLabel.text = NSLocalizedString(@"CostoDiscendente", @"");
        }
        if ([[defaults objectForKey:@"ordinaPer"] isEqualToString:@"Categoria Discndente"]) {
            cell.detailTextLabel.text = NSLocalizedString(@"CategoriaDiscendente", @"");
        }

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    if (indexPath.section == 0 && indexPath.row == 1) {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        cell.textLabel.text = NSLocalizedString(@"DettagliDesiderio", @"");

        if ([[defaults objectForKey:@"dettagliView"] isEqualToString:@"costoView"]) {
            cell.detailTextLabel.text = NSLocalizedString(@"Costo", @"");
        }
        if ([[defaults objectForKey:@"dettagliView"] isEqualToString:@"descrizioneView"]) {
            cell.detailTextLabel.text = NSLocalizedString(@"Descrizione", @"");
        }
        if ([[defaults objectForKey:@"dettagliView"] isEqualToString:@"urlView"]) {
            cell.detailTextLabel.text = NSLocalizedString(@"URL", @"");
        }

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    if (indexPath.section == 1 && indexPath.row == 0) {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        cell.textLabel.text = NSLocalizedString(@"Shortener", @"");

        if ([[defaults objectForKey:@"linkShortener"] isEqualToString:@"Nessuno"]) {
            cell.detailTextLabel.text = NSLocalizedString(@"Nessuno", @"");
        }
        if ([[defaults objectForKey:@"linkShortener"] isEqualToString:@"is.gd"]) {
            cell.detailTextLabel.text = NSLocalizedString(@"is.gd", @"");
        }
        if ([[defaults objectForKey:@"linkShortener"] isEqualToString:@"bit.ly"]) {
            cell.detailTextLabel.text = NSLocalizedString(@"bit.ly", @"");
        }
        if ([[defaults objectForKey:@"linkShortener"] isEqualToString:@"TinyURL"]) {
            cell.detailTextLabel.text = NSLocalizedString(@"TinyURL", @"");
        }
        if ([[defaults objectForKey:@"linkShortener"] isEqualToString:@"Linkyy"]) {
            cell.detailTextLabel.text = NSLocalizedString(@"Linkyy", @"");
        }

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    if (indexPath.section == 2 && indexPath.row == 0) {
        cell.textLabel.text = @"Access Code";

        codeSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 84, 27)];
        cell.accessoryView = codeSwitch;
        [codeSwitch addTarget:self action:@selector(addCellToSetCode:) forControlEvents:UIControlEventValueChanged];
        codeSwitch.on = [[NSUserDefaults standardUserDefaults] boolForKey:@"codeSwitchState"];

        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (indexPath.section == 3 && indexPath.row == 0) {
        cell.textLabel.text = NSLocalizedString(@"Supporto", @"");
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    if (indexPath.section == 3 && indexPath.row == 1) {
        cell.textLabel.text = NSLocalizedString(@"Informazioni", @"");
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    return cell;
}

编辑:以下更新...

我解决了这个问题的一部分!
我尝试使用[self.tableView reloadData]但不起作用,随便我使用[self.tableView setNeedsDisplay]解决...

现在开关工作但是如果我设置为On然后我从应用程序出去,完全关闭它,开关不起作用......我怎么能解决这个问题?

如果这可以帮助其他人,这些代码片段已更新:

-(void)addCellToSetCode:(id)sender {

    if ([codeSwitch isOn]) {

        NSIndexPath *updatedIndexPath = [NSIndexPath indexPathForRow:1 inSection:2];
        [self.tableView beginUpdates];
        [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:updatedIndexPath] withRowAnimation:UITableViewRowAnimationTop];
        [self.tableView endUpdates];

        [self.tableView setNeedsDisplay];

        [[NSUserDefaults standardUserDefaults] setBool:codeSwitch.on forKey:@"codeSwitchState"];
    }
    else {

        NSIndexPath *updatedIndexPath = [NSIndexPath indexPathForRow:1 inSection:2];
        [self.tableView beginUpdates];
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:updatedIndexPath] withRowAnimation:UITableViewRowAnimationTop];
        [self.tableView endUpdates];

        [self.tableView setNeedsDisplay];

        [[NSUserDefaults standardUserDefaults] setBool:codeSwitch.on forKey:@"codeSwitchState"];
    }
}

// tableView:numberOfRowsInSection:
else if (section == 2) {
    return codeSwitch.on ? 2 : 1;
}

// tableView:cellForRowAtIndexPath:
if (indexPath.section == 2 && indexPath.row == 0) {
    cell.textLabel.text = @"Access Code";

    codeSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 84, 27)];
    cell.accessoryView = codeSwitch;
    [codeSwitch addTarget:self action:@selector(addCellToSetCode:) forControlEvents:UIControlEventValueChanged];
    codeSwitch.on = [[NSUserDefaults standardUserDefaults] boolForKey:@"codeSwitchState"];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
if (indexPath.section == 2 && indexPath.row == 1) {
    if ([codeSwitch isOn]) {
        cell.textLabel.text = @"Set Access Code";
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
}

1 个答案:

答案 0 :(得分:1)

至少部分问题(在更新的代码中)是您在创建单元格之前不创建UISwitch。您的codeSwitch ivar可能最终指向不同的开关,因为该表格行进入和退出视图。

以下是我如何执行此操作:在tableView:numberOfRowsInSection:中,使用NSUserDefaults查看表应该处于哪种状态,而不是使用交换机的状态(可能尚不存在)。然后,在switch的action方法中,在插入或删除表行之前,为用户默认调用setBool:forKey:

从本质上讲,这使得代码更好地遵循MVC模型,因为它将视图(UISwitch)与模型(用户默认值中的BOOL)分开,在中间使用控制器(视图控制器)。通过混淆视图和模型(开关和布尔状态),当视图不可用时,在尝试处理状态时最终会出现问题。

顺便说一句,您根本不需要在表格视图上调用setNeedsDisplay