在UITableView中的Handeling单uitextfield
我在单元格分配部分有一个单独的textField,在本节之后,我尝试提供给文本字段各种参数。 工作正常没有问题,直到这里,问题在于如何处理哪些textField在委托方法中返回。在UITableView中的Handeling单uitextfield
我之前的做法是简单地为不同的用户输入分配不同的文本字段,但是当有很多textField时会产生UI毛刺,因此要避免它。
更好地了解这里是表委托方法cellAtIndexRow示例代码
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellTableIdentifier = @"CellTableIdentifier";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellTableIdentifier];
if (cell == nil) {
// Create a new cell. CGRectZero allows the cell to determine the appropriate size.
CGRect cellFrame = CGRectMake(0,0,300,65);
cell = [[[UITableViewCell alloc] initWithFrame:cellFrame reuseIdentifier:CellTableIdentifier] autorelease];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(8,4,284,25)];
textField.delegate = self;
textField.returnKeyType = UIReturnKeyDone;
textField.tag = kTagAddContactTextField;
textField.backgroundColor = [UIColor orangeColor];
[cell.contentView addSubview:textField];
[textField release];
}
UITextField *textField = (UITextField*)[cell.contentView viewWithTag:kTagAddContactTextField];
switch (indexPath.row) {
case 0:
[textField setPlaceholder:@"First Name"];
break;
case 1:
[textField setPlaceholder:@"Last Name"];
break;
case 2:
[textField setPlaceholder:@"Email"];
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
textField.keyboardType = UIKeyboardTypeEmailAddress;
break;
}
//cell.textLabel.text = @"Test";
return cell;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
//here is the place where I wann to handle various text fields and store there values.
return YES;
}
尝试拉
textField.tag = kTagAddContactTextField;
出来的表格单元格中创建代码,并为每一行分配不同的标记值你的UITable。
switch (indexPath.row) {
case 0:
textField.tag = kFirstNameField;
[textField setPlaceholder:@"First Name"];
break;
case 1:
textField.tag = kLastNameField;
[textField setPlaceholder:@"Last Name"];
break;
case 2:
textField.tag = kEmail;
[textField setPlaceholder:@"Email"];
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
textField.keyboardType = UIKeyboardTypeEmailAddress;
break;
}
然后,您可以区分返回标记所获得的文本字段。
你可以使用UIView的“标签”属性(UITextField继承)。
例如
switch (indexPath.row) {
case 0:
[textField setPlaceholder:@"First Name"];
textField.tag=1;//Name
break;
case 1:
[textField setPlaceholder:@"Last Name"];
textField.tag=2;//Surname
break;
case 2:
[textField setPlaceholder:@"Email"];
textField.tag=3;//Email
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
textField.keyboardType = UIKeyboardTypeEmailAddress;
break;
}
,并在回调:
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
//here is the place where I wann to handle various text fields and store there values.
switch(textField.tag)
{
case 1://Name
// do stuff;
break;
case 2://Surname
// do stufff;
break;
case 3://Email
//do stuff;
break;
}
return YES;
}
除非你需要搜索全部三个标签来找到textField,以便重新设置它到正确的一个(在小区重用的情况下)。 – 2010-06-28 15:41:33
在可重用的情况下,您始终可以从您的单元格中删除子视图,但这些标签始终是从其任务中修复的。 – Sanniv 2010-07-06 07:37:02
只要用户选择了编辑didSelectRowAtIndexPath
一个文本框应该被调用。在那里你可以切换值为一个int属性currentEdited
,从中可以检测到用户正在改变的值。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
switch(indexPath.row)
{
case 0:currentEditing = kEditingFirstName;
break;
case 1:....
...
}
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
switch(currentEditing)
{
case kEditingFirstName:
//store first name
break;
....
}
return YES;
}
在其他答案中使用的标记事项可能会导致问题,因为重新使用文本字段。
当你触摸文本字段时,并不总是如此,didSelectRowAtIndexPath将不会被调用,但适当的文本字段委托 – Ameya 2010-06-28 09:35:37
不同ickiness的几种方法:
- 使用不同的重用标识符和标签为每个种类的细胞,并在标签上进行切换。这意味着细胞不会被重用,因为它们可能是,但这不是世界的尽头。
- 请关注UIView.superview,直到找到UITableViewCell的一个子类(textField.superview.superview应该可以工作,但这可能会在未来版本的iPhone OS中更改),然后使用[tableView indexPathForRow:cell]。
- 使用不同的重用标识符(但标签相同),遵循超视图链,直到找到UITableViewCell并查看其重用标识符。
- 使用存储NSString *或NSInteger键的自定义UITableViewCell子类,以便知道它是什么类型的单元格(可选地,使单元成为文本字段的委托并让单元向视图控制器通知更改)。
- 交换机上的占位符文本(很恶心,当涉及到的i18n)
任何代码段代码为我上面的情况。 – Ameya 2010-06-29 05:05:21
请注意,您需要设置autocorrectionType/autocapitalizationType /键盘类型行0和1的情况下,从第2行的单元被重用。 – 2010-06-28 15:42:27