Question

I have a alert view with a textfield so whenever someone types in it and pressed the save button it puts in the table view. when you click on the cell after being saved it takes you to a different view. Now when you go back to the home page, the cells disappear. I have tried multiple ways of figuring it out, yet still haven't been able to. Do I need to add a plist so every time I add a cell it gets saved to the plist and if so where would i start?

This code is in my table view controller

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

    }

}
Was it helpful?

Solution

I think tableData is getting deallocated when you are coming back to the home page. If the tableData is not a huge array you can store it in NSUserDefaults. This way when ever you come back to the table you can always retreat the data. Check out this code. It will store the tableData in the NSUserDefaults every time you add anything to the array. Let me know if this works for you.

#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;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top