如何将字段数据保存在字典中,然后保存在数组中?

问题描述:

一旦用户完成在UITextfield中输入文本,我会先将数据放入字典中,然后将字典添加到数组中。但对于插入阵列后,由于某种原因。它记录为空..如何将字段数据保存在字典中,然后保存在数组中?

.H

@interface Bread_TableViewController : UITableViewController <UITextFieldDelegate> 
{  
    NSMutableArray * inventoryarray; 
} 

**.m** 

- (void)textFieldDidEndEditing:(UITextField *)textField 
{ 
    // Make sure to set the label to have a ta 
    NSString*textfieldclicked; 

    if (textField.tag == 1) { 
     [email protected]"Unit"; 
    } else if (textField.tag ==2) { 
     [email protected]"Case"; 

    } 

    id textFieldSuper = textField; 

    while (![textFieldSuper isKindOfClass:[UITableViewCell class]]) { 

     textFieldSuper = [textFieldSuper superview]; 

    } 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)textFieldSuper]; 

    InventoryTableViewCell *cell = (InventoryTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath]; 


    NSMutableDictionary * productinfo = [[NSMutableDictionary alloc] init]; 

    [productinfo setObject:cell.product.text forKey:@"product"]; 
    [productinfo setObject:textField.text forKey:textfieldclicked]; 

    NSLog(@"productin %@", productinfo);  
    [inventoryarray addObject: productinfo]; 

} 

-(IBAction)save{ 

    NSLog(@"array %@", inventoryarray); 

} 
+0

猪头,ALLOC UR-INIT? –

+0

只是固定的....但是我的数组一次只显示一个字典..:/ – ASH

+0

请看看我的回答 –

  1. alloc-init要在其中添加对象的数组。
  2. textFieldDidEndEditing方法外你的字典中的alloc-init和地方范围之可见可能是viewDidLoad

 @interface yourViewController() 
      @property (strong, nonatomic) NSMutableDictionary * product info; 

     - (void)viewDidLoad { 
     self.productinfo = [[NSMutableDictionary alloc] init]; 
     } 
+0

作品谢谢.. – ASH

不可见的细胞实际上并不存在。只有可见的加上每个屏幕外的一个实际上在内存中。其他的是在你滚动时创建的,它们的数据是从数据源中提取的。
一旦一个单元格被移出屏幕(+1单元格),它将从层次结构中移除并添加到重用池中。

TL; DR:您不能在屏幕外单元之间循环,因为它们不存在。如果您想访问每个项目的数据,请在您的数据源中执行。

+1

当一个单元格关闭屏幕时,它不一定会被解除分配,但它会从视图层次结构并添加到单元重用池中。稍微迂腐,我知道。很好的答案和upvoted。 –

+0

你是对的,我编辑了我的答案以反映你的评论。谢谢 –

+0

感谢您的回复..不确定您在这种情况下数据源的含义...因为我需要抓取用户在单元格中的文本字段中输入的文本。 – ASH