2015-09-04 50 views
0

一旦用戶完成在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

請看看我的回答 –

回答

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

不可見的細胞實際上並不存在。只有可見的加上每個屏幕外的一個實際上在內存中。其他的是在你滾動時創建的,它們的數據是從數據源中提取的。
一旦一個單元格被移出屏幕(+1單元格),它將從層次結構中移除並添加到重用池中。

TL; DR:您不能在屏幕外單元之間循環,因爲它們不存在。如果您想訪問每個項目的數據,請在您的數據源中執行。

+1

當一個單元格關閉屏幕時,它不一定會被解除分配,但它會從視圖層次結構並添加到單元重用池中。稍微迂腐,我知道。很好的答案和upvoted。 –

+0

你是對的,我編輯了我的答案以反映你的評論。謝謝 –

+0

感謝您的回覆..不確定您在這種情況下數據源的含義...因爲我需要抓取用戶在單元格中的文本字段中輸入的文本。 – ASH

相關問題