【发布时间】:2011-10-21 04:42:08
【问题描述】:
我正在尝试拥有一个 UITableView,我可以从中动态添加和删除行,并且这些行中有一个 UITextField。为了添加行,我使用的是代码:
- (void) addRow
{
[nameArray addObject:[NSString stringWithFormat:@"%i", x]];
[self.tableView reloadData];
x++;
}
我只是在计算 nameArray 以获取我的 tableView 中有多少行。然后,在 cellForRowAtIndexPath 内部,我得到了以下代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
/*Default Apple-y stuff*/
...
if ([indexPath row] == 0) {
playerTextFieldZero = [[UITextField alloc] initWithFrame:CGRectMake(20, 10, 185, 30)];
playerTextFieldZero.adjustsFontSizeToFitWidth = YES;
playerTextFieldZero.placeholder = @"Name";
playerTextFieldZero.textColor = [UIColor blackColor];
playerTextFieldZero.returnKeyType = UIReturnKeyDone;
playerTextFieldZero.backgroundColor = [UIColor whiteColor];
playerTextFieldZero.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
playerTextFieldZero.textAlignment = UITextAlignmentLeft;
playerTextFieldZero.tag = 0;
playerTextFieldZero.delegate = self;
playerTextFieldZero.clearButtonMode = UITextFieldViewModeNever; // no clear 'x' button to the right
[playerTextFieldZero setEnabled: YES];
[cell addSubview:playerTextFieldZero];
[playerTextFieldZero becomeFirstResponder];
[playerTextFieldZero release];
}
...
/*More of those in here*/
...
return cell;
}
此代码存在多个问题。第一个问题是我正在做一个预设数量的 UITextField,这样我就可以在 textFieldShouldReturn 中调用它们。有没有一种好方法可以生成当我按下完成键时返回的 UITextFields?
我现在这样做的第二大问题是我的 UITextFields 每次添加新的时都会被清除。知道为什么吗?
【问题讨论】:
标签: iphone xcode uitableview uitextfield