申し訳ありませんが、私はこの質問が以前に尋ねられたことは知っていますが、私の問題は他のものとは異なります。UITableFieldをNSMutableArrayのUITableViewCellに保存します。
数字パッドを閉じた後に配列に保存する必要があるカスタムテーブルビューセルの内部にテキストフィールドがあります。ナンバーパッドには完了ボタンがないので、ナンバーパッドを閉じるためにタッチイベントを実装する必要がありました。つまり、私の-textFieldDidEndEditingは起動していません。ここでは、関連するコードは次のとおりです。あなたのタッチイベントは、キーボードを閉じるときに、私のviewController.mの私viewController.h
itemData *tempItem; //A custom object
UITableView *itemsListTable;
NSMutableArray *listData;
で
- (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];
}
また、適切な命名規則のために、すべてのクラスの名前は大文字で始める必要があります。 'itemData'の代わりに' ItemData'を使用します。 – Raptor
@ ShivanRaptor私はUITableViewまたはUITextFieldに関するコードを投稿しました – Boos1993