将UITextField保存在UITableViewCell内部到NSMutableArray

时间:2012-03-22 02:34:44

标签: iphone ios uitableview nsmutablearray uitextfield

好吧,我之前已经问过这个问题,但我的问题与其他问题不同。

我在自定义tableview单元格中有一个文本字段,我需要在数字键盘被关闭后保存到数组中。由于数字键盘没有完成按钮,我不得不实现触摸事件来关闭数字键盘。这意味着我的-textFieldDidEndEditing没有触发。以下是相关代码:

在我的viewController.h

itemData *tempItem; //A custom object    
UITableView *itemsListTable;
NSMutableArray *listData;

在我的viewController.m

    - (UITableViewCell *)tableView:(UITableView *)tableView
             cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {

       static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";


        UITableViewCell *cell = [itemsListTable
                                 dequeueReusableCellWithIdentifier:SimpleTableIdentifier];

        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
                                          reuseIdentifier:SimpleTableIdentifier];


        NSInteger row = indexPath.row;
        tempItem = [listData objectAtIndex:row];
        cell.textLabel.text = [tempItem getItemName];


        UITextField *quantityTextField = [[UITextField alloc] initWithFrame:CGRectMake(275, 8, 35, 27)];
        quantityTextField.keyboardType = UIKeyboardTypeNumberPad;
        quantityTextField.borderStyle = UITextBorderStyleRoundedRect;
        quantityTextField.returnKeyType = UIReturnKeyDone;
        quantityTextField.text = [NSString stringWithFormat:@"%d", tempItem.getItemQuantity];
        quantityTextField.textAlignment = UITextAlignmentCenter;


        [cell addSubview:quantityTextField];
        } 


        return cell;

    }

-(void)touchesEnded: (NSSet *)touches withEvent: (UIEvent *)event {
[[self itemsListTable] endEditing:YES];
    }
- (void)textFieldDidEndEditing:(UITextField *)textField {

    UITableViewCell *cell = (UITableViewCell*)textField.superview;

    NSIndexPath *indexPath = [self.itemsListTable indexPathForCell:cell];

    tempItem = [listData objectAtIndex:indexPath.row];

    NSLog(@"Did End Editing");
    [tempItem setItemQuantity:[textField.text intValue]]; //save textfield to object as intValue

    [listData replaceObjectAtIndex:indexPath.row withObject:tempItem]; //save object to array
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [listData removeObjectAtIndex:indexPath.row];
        [itemsListTable reloadData];
    }    
}

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{
    numberOfItems.text = [NSString stringWithFormat:@"%d",listData.count];

    return [self.listData count];
}

1 个答案:

答案 0 :(得分:2)

当您的触摸事件取消键盘时,也可以调用[self textFieldDidEndEditing:],或者如果您需要知道哪个文本字段最后有焦点,请通过定义您设置为当前文本字段的UITextField对象来保留引用textFieldDidBeginEditing委托函数中的第一个响应者。像这样:

//In .h
UITextField * txtReference;

//In .m
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    txtReference = textField;
}

//In the function that dismisses the keyboard put:
[self textFieldDidEndEditing:txtReference];