2015-09-04 28 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已经将其定义了。