Iphone 获取/设置表单元格中的UITextField值
对于我当前的项目,我需要有一个表,其中每个单元格中都包含textfield,单元格和textfield的数量必须是动态的,这取决于相互数组中的数据数量。我在单元格中设置了textfield,但无法获取/设置textfield值。我想知道你们是否能帮我,或者至少纠正我做错了什么?谢谢,这是很早以前的事。请参见下面的代码片段:Iphone 获取/设置表单元格中的UITextField值,iphone,ipad,uitableview,uitextfield,Iphone,Ipad,Uitableview,Uitextfield,对于我当前的项目,我需要有一个表,其中每个单元格中都包含textfield,单元格和textfield的数量必须是动态的,这取决于相互数组中的数据数量。我在单元格中设置了textfield,但无法获取/设置textfield值。我想知道你们是否能帮我,或者至少纠正我做错了什么?谢谢,这是很早以前的事。请参见下面的代码片段: // Adds textfield into cell - (UITableViewCell *)tableView:(UITableView *)tableView ce
// Adds textfield into cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSUInteger row = indexPath.row;
X10ArchiefIndexDefs *curIndex = [indexDefinities objectAtIndex:row];
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
BOOL bShowSelection = ([curIndex.HasVasteWaarden isEqualToString:@"false"]);
if (bShowSelection) {
bShowSelection = !([curIndex.DataType isEqualToString:@"Datum"]);
}
if ([indexPath section] == 0) {
if (bShowSelection) {
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
UITextField *editField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
editField.adjustsFontSizeToFitWidth = YES;
editField.textColor = [UIColor blackColor];
editField.placeholder = curIndex.Naam;
editField.keyboardType = UIKeyboardTypeDefault;
editField.returnKeyType = UIReturnKeyNext;
editField.backgroundColor = [UIColor whiteColor];
editField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
editField.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support
editField.textAlignment = UITextAlignmentLeft;
editField.clearButtonMode = UITextFieldViewModeNever; // no clear 'x' button to the right
editField.tag = [curIndex.UID intValue];
[editField setEnabled: YES];
[cell addSubview:editField];
[editField release];
}
}
return cell;
}
在某些情况下,我使用popovercontroller来显示数据列表。用户可以选择弹出窗口的值。当选择了一个值时,执行此代码:
- (void)selectedValue:(NSString *) value {
//---update value of the text field ---
//The first attempt it doesn't put the text to text field
//static NSString *CellIdentifier = @"Cell";
//UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//
//if (cell == nil) {
// cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
//}
// second attempt it crashes
X10ArchiefIndexDefs *curIndex = [indexDefinities objectAtIndex:curRow.row];
int index = [curIndex.UID intValue];
UITextField *textField = (UITextField *) [curCell viewWithTag: index];
if (textField) {
[textField setText:value];
}
[textField release];
[self.popOverController dismissPopoverAnimated:YES];
}
选择单元格后,我将确保保存该单元格以供以后使用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
X10ArchiefIndexDefs *curIndex = [indexDefinities objectAtIndex:indexPath.row];
if (!curIndex) {
return;
}
curRow = indexPath; // saves the selected row
if ([curIndex.VasteWaarden count] > 0) {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
curCell = cell; // saves the selected cell
CGRect frame = [cell.superview convertRect:cell.frame toView:self.view];
self.detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
detailViewController.delegate = self;
self.popOverController = [[[UIPopoverController alloc] initWithContentViewController:detailViewController] autorelease];
X10ArchiefIndexDefs *curIndex = [indexDefinities objectAtIndex:indexPath.row];
self.detailViewController.Values = curIndex.VasteWaarden;
[self.popOverController presentPopoverFromRect:frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
再次感谢你,这是非常提前的
干杯,
在第二个代码段中,您正在释放文本字段。你不应该这样做,因为你没有保留它。因为
viewWithTag:
simple获取对文本字段的引用,所以它不保留文本字段。因此,您释放它的次数比保留的次数多,因此retainCount
达到0,并且文本字段从内存中解除分配。然后,当您第二次尝试时,内存中没有文本字段
只需删除:
[textField release];
来自第二个代码段。如果你不明白为什么,那就读一些关于内存管理的文章(只需谷歌一下)。要完全理解它需要一些时间,至少我知道我花了一些时间:)非常感谢您的快速回复。我删除了这个,现在它工作得很好。:)