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 回答