【发布时间】:2011-09-13 04:46:57
【问题描述】:
我有一个 3 行的表格视图。每行都有相同的自定义 tableviewcell 和一个 uitextfield 但占位符属性不同。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"NewDestinationCell";
NewDestinationCell *cell = (NewDestinationCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
[cellNib instantiateWithOwner:self options:nil];
cell = editCell;
self.editCell = nil;
cell.detailTextField.delegate = self;
cell.detailTextField.tag = indexPath.row;
[cell.detailTextField setInputAccessoryView:keybdToolbar];
}
if ([[textFieldStrings objectAtIndex:indexPath.row] length] != 0)
cell.detailTextField.text = [textFieldStrings objectAtIndex:indexPath.row];
return cell;
}
当我点击文本字段并输入一些文本时,转到另一个文本字段,然后返回输入文本的文本字段,它会删除我输入的内容并放置占位符文本。我必须实现 commitEditingStyle 方法吗?每个表格行中的文本字段都链接到同一个 uitextfield 出口。也许这就是为什么?这是我用来遍历三行的代码。
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[textField becomeFirstResponder];
textField.text = [textFieldStrings objectAtIndex:textField.tag];
currTextField = textField;
if (currTextField.tag == [self.tableView numberOfRowsInSection:0]-1) {
NextButton.enabled = NO;
PrevButton.enabled = YES;
}
if (currTextField.tag == 0) {
PrevButton.enabled = NO;
NextButton.enabled = YES;
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
[textFieldStrings replaceObjectAtIndex:textField.tag withObject:textField.text];
[textField resignFirstResponder];
}
- (IBAction)PrevTextField:(id)sender {
[tableViewCellFields[currTextField.tag-1] becomeFirstResponder];
}
- (IBAction)NextTextField:(id)sender {
[tableViewCellFields[currTextField.tag+1] becomeFirstResponder];
}
【问题讨论】:
-
一些格式会很好。代码也会有所帮助。
标签: ios uitextfield placeholder