2015-09-04 13 views
-1

我在textFieldDidEndEditing上插入文本字段數據到數組中。如何替換NSARRAY中值和鍵相等的數據?

但我不想重複。如果用戶更改了文本字段數據,我希望它被替換爲新的並更新了字典中的名稱爲「product」的值爲「bagel」的名稱。

.H

@interface Bread_TableViewController : UITableViewController <UITextFieldDelegate> 
{ 


    NSMutableArray *productarray; 


} 

.M

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    productarray = [NSMutableArray array]; 

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


    NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:textField.text,textfieldclicked,cell.product.text,@"product",nil]; 


    [productarray addObject:plainPart]; 

} 

回答

0

你可以嘗試這樣的事情也許:

​​
+0

由於未捕獲的異常「NSRangeException」而終止應用,原因:'*** - [__ NSArrayM replaceObjectAtIndex:withObject:]:index 2147483647超出空數組的邊界' – ASH

0

您可以使用NSMutableOrderedSet,其工作方式一個NSArray,但它保證元素的唯一性。

你不必擔心執行-hash-isEqual:爲你使用NSStringNSDictionary已經將其定義了。

相關問題