如何在Tableview中使用UITextField

我认为在Tableview中使用UITextField的最大难题就是如何找到Textfield以及在面对TableviewCell的重用机制时的数据同步问题。
以下摘自http://blog.sina.com.cn/s/blog_7d018cc601018ya5.html
首先需要解决的问题就是如果textfield比较靠下,那么键盘弹出会挡住这个cell。
解决方法:
由于TableView继承自UIScrollView使其滚动到上面即可,所以重写UITextFieldDelegate的方法

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    if(textField==userNameTextField) //或者根据tag if(textField.tag)
    [_tableview setContentOffset:CGPointMake(0, 100) animated:YES];
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    [_tableview setContentOffset:CGPointMake(0, 0) animated:YES];
}

或者在初始化UITextField对象实例时添加事件

[checkCodeTextField addTarget:self action:@selector(upLayout) forControlEvents:UIControlEventEditingDidBegin];
[checkCodeTextField addTarget:self action:@selector(downLayout) forControlEvents:UIControlEventEditingDidEnd];

关于文本框的输入
在一个文本框输入结束返回的时候,释放自身的响应级别resignFirstResponder,让下一个文本框成为焦点,即变成第一响应控件becomeFirstResponder,具体为:重写UITextFieldDelegate方法。下面的写法前提是在定义文本框时将其tag设为相连的几个整值。
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
if([self.view viewWithTag:textField.tag+1])
[[self.view viewWithTag:textField.tag+1] becomeFirstResponder];
return YES;
}




UITableViewCell的contentView中的UITextField的值获取有几种方法,本人简单总结一下。



1. 获取UITextField所以Cell的NSIndexPath,知道了NSIndexPath就知道了这个UITextField是干什么的了。

可以在

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

{
//get cell
UITableViewCell *cell = [UITableViewCell ][[textField superview] superview];
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
}



- (void)textFieldDidEndEditing:(UITextField *)textField

{
//get cell
UITableViewCell *cell = (UITableViewCell *)[[textField superview] superview];
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
}

中得知道UITextField中text是哪一个数据结构的值,前一个是实时的,后一个是失去焦点时一次性的。

2。第二种方法与上面第一个有点类似也是实时的,来自:http://blog.sina.com.cn/s/blog_9ca91e4a0100xlvu.html

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
NSInteger row = [indexPath row];

static NSString *CellIdentifier = @"CellIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}

cell.textLabel.text = [_passwordArray objectAtIndex:row];

CGRect textFieldRect = CGRectMake(0.0, 0.0f, 215.0f, 31.0f);
UITextField *theTextField = [[UITextField alloc] initWithFrame:textFieldRect];
theTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
theTextField.returnKeyType = UIReturnKeyDone;
theTextField.secureTextEntry = YES;
theTextField.clearButtonMode = YES;
theTextField.tag = row;
theTextField.delegate = self;

//此方法为关键方法
[theTextField addTarget:self action:@selector(textFieldWithText:) forControlEvents:UIControlEventEditingChanged];

switch (row) {
case 0:
theTextField.placeholder = @"请输入旧密码";
break;
case 1:
theTextField.placeholder = @"请输入新密码";
break;
case 2:
theTextField.placeholder = @"请再次输入新密码";
break;
default:
break;
}

cell.accessoryView = theTextField;
[theTextField release];

return cell;
}

- (void)textFieldWithText:(UITextField *)textField
{
switch (textField.tag) {
case 0:
self.theOldPassword = textField.text;
break;
case 1:
self.theNewPassword = textField.text;
break;
case 2:
self.theTwiceNewPassword = textField.text;
break;
default:
break;
}
}

评论

此博客中的热门博文

使__attribute__((annotate("xxx")))能作用于OC函数

viewWillAppear不执行的解决办法

iOS中使用xpc/xpc.h