Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 将UITableViewCell内的UITextField保存到NSMutableArray_Iphone_Ios_Uitableview_Nsmutablearray_Uitextfield - Fatal编程技术网

Iphone 将UITableViewCell内的UITextField保存到NSMutableArray

Iphone 将UITableViewCell内的UITextField保存到NSMutableArray,iphone,ios,uitableview,nsmutablearray,uitextfield,Iphone,Ios,Uitableview,Nsmutablearray,Uitextfield,好吧,我知道以前有人问过这个问题,但我的问题与其他问题不同 在自定义tableview单元格中有一个文本字段,我需要在数字键盘关闭后将其保存到数组中。由于数字键盘没有“完成”按钮,我必须实现一个触摸事件来关闭数字键盘。这意味着我的-textfielddidediting没有启动。以下是相关代码: 在我的viewController.h中 itemData *tempItem; //A custom object UITableView *itemsListTable; NSMutable

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

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

在我的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-TextFieldDiEndediting:],或者如果需要知道哪个文本字段最后有焦点,请通过定义一个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];

此外,为了良好的命名约定,所有类的名称都应该以大写字母开头,例如,
ItemData
,而不是
ItemData
@ShivanRaptor I仅发布与UITableView或uitextfield相关的代码谢谢!这对我的要求非常有效!不幸的是,我意识到我需要一种不同的方法,因为我在每个单元格中都有一个文本字段;)如果每个单元格中都有UITextfield,那么如果还没有自定义单元格,您是否考虑过创建自定义单元格。