iphone - 将 UITableViewCell 中的 UITextField 保存到 NSMutableArray

标签 iphone ios uitableview nsmutablearray uitextfield

好吧,我知道之前有人问过这个问题,但我的问题与其他问题不同。

我在自定义表格 View 单元格内有一个文本字段,我需要在关闭数字键盘后将其保存到数组中。由于数字键盘没有完成按钮,我不得不实现一个触摸事件来关闭数字键盘。这意味着我的 -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];
}

最佳答案

当你的触摸事件关闭键盘时,也调用 [self textFieldDidEndEditing:],或者如果你需要知道哪个文本字段最后获得焦点,通过定义一个你设置为当前文本字段的 UITextField 对象来保持引用textFieldDidBeginEditing 委托(delegate)函数中的第一响应者。像这样:

//In .h
UITextField * txtReference;

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

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

关于iphone - 将 UITableViewCell 中的 UITextField 保存到 NSMutableArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9815660/

相关文章:

iphone - 如何左对齐 CCMenuItems?

android - Html5输入类型数字格式化

iphone - IB中的UITableViewCell和Programmatically,哪个更适合定制?

iphone - Quartz PDF API 导致内存不足崩溃

ios - 我可以观察 UIViewController 何时更改 interfaceOrientation 吗?

ios - 什么使用 Passbook 的日志记录端点?

ios - 带有图像背景的 iOS 7 中的 UITableViewCell 问题

ios - 带有 didChangeObject 的 NSFetchedResultsController 在表格 View 中显示单元格的速度很慢

iOS Swift 过滤器和生成模型需要太多时间

ios - 表格 View 单元格仅在滚动关闭然后重新打开后才能正确屏蔽