0

テキストフィールドを備えたアラートビューがあるため、誰かが入力して保存ボタンを押すと、テーブルビューに表示されます。保存後にセルをクリックすると、別のビューに移動します。ホームページに戻ると、セルが消えます。私はそれを理解するために複数の方法を試しましたが、まだできていません。セルを追加するたびに plist に保存されるように、plist を追加する必要がありますか?

このコードは私のテーブルビューコントローラーにあります

- (IBAction)add:(id)sender {

NSLog(@"%@",tableData);
UIAlertView* alert=[[UIAlertView alloc] initWithTitle:@"My Favs" message:@"Hello" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Save", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * alertTextField = [alert textFieldAtIndex:0];
alertTextField.enablesReturnKeyAutomatically = YES;
alertTextField.placeholder = @"example";
[alert show];
return;


}


-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
        NSLog(@"%@",tableData);
    //Only do the following action if the user hits the ok button
    if (buttonIndex == 1){

                NSString *tapTextField = [alertView textFieldAtIndex:0].text;

                if (!tableData)
                        {
                tableData = [[NSMutableArray alloc]init];
                                           }

                [tableData insertObject:tapTextField atIndex:0];

                [myTableView reloadData];

    }

}
4

1 に答える 1

0

tableDataホームページに戻ってきたときに割り当てが解除されていると思います。が巨大な配列でない場合は、tableDataに格納できますNSUserDefaults。このようにして、テーブルに戻ったときはいつでもデータを取り消すことができます。このコードをチェックしてください。tableData配列に何かを追加するたびに、NSUserDefaults に格納されます。これがうまくいくかどうか教えてください。

#import "ViewController.h"

    @interface ViewController () <UIAlertViewDelegate,UITableViewDataSource,UITableViewDelegate>

    @property (weak, nonatomic) IBOutlet UITableView *myTableView;
    @property (nonatomic,strong) NSMutableArray *tableData;
    @end

    @implementation ViewController


    -(NSMutableArray *)tableData
    {
        if(!_tableData){
            NSMutableArray *data = [[NSUserDefaults standardUserDefaults]objectForKey:@"data"];
            if(!data){
                _tableData = [[NSMutableArray alloc]init];
            }else{
                _tableData = [data mutableCopy];
            }
        }
        return _tableData;
    }


    - (IBAction)add:(UIButton *)sender {
        UIAlertView* alert=[[UIAlertView alloc] initWithTitle:@"My Favs" message:@"Hello" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Save", nil];
        alert.alertViewStyle = UIAlertViewStylePlainTextInput;
        UITextField * alertTextField = [alert textFieldAtIndex:0];
        alertTextField.enablesReturnKeyAutomatically = YES;
        alertTextField.placeholder = @"example";
        [alert show];
    }

    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        if (buttonIndex == 1){

            NSString *tapTextField = [alertView textFieldAtIndex:0].text;
            [self.tableData insertObject:tapTextField atIndex:0];
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            [defaults  setObject:self.tableData forKey:@"data"];
            [defaults synchronize];
            [self.myTableView reloadData];

        }

    }

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return[self.tableData count];
    }
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }


    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"dataCell" forIndexPath:indexPath];
        if(self.tableData.count > 0){
            cell.textLabel.text = self.tableData[indexPath.row];

        }
        return cell;
    }
于 2013-08-25T03:59:39.960 に答える