我在单元格分配部分有一个textField,在本节之后我尝试为文本字段提供各种参数。 工作没问题,直到这里,问题在于如何处理委托方法中返回的textField。
我早期的方法是简单地为不同的用户输入分配不同的文本字段,简单但在有很多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;
}
答案 0 :(得分:0)
尝试拉
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;
}
然后,您可以通过返回的标记来区分您获得的文本字段。
答案 1 :(得分:0)
你可以使用UIView的“tag”属性(UITextField继承)。
e.g。
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;
}
答案 2 :(得分:0)
改变ickiness的几种方法:
答案 3 :(得分:-1)
每当用户选择要编辑的文本字段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;
}
其他答案中使用的标记内容可能会因为重复使用文本字段而导致问题。