2013-08-24 28 views
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]; 

    } 

} 

回答

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; 
    } 
+0

解决了一些代码后,它的工作完全谢谢,但我测试了它的删除方法,它不断回来,当我去到主页后被删除。你如何直接从NSUserDefault中删除它? @Yan – Maroun

+0

从来没有我实际上通过把NSUserDefaults *默认值= [NSUserDefaults standardUserDefaults]; [默认removeObjectFor – Maroun

+0

这太好了。关于删除方法,您可以从tableData数组中删除项目,然后将其保存回NSUserDefaults,而不是从中删除tableData。 – Yan

相关问题