iOS之获取UITableViewCell中UITextField的值

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

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

可以在

  1. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string  
  2. {  
  3. //get cell  
  4. UITableViewCell *cell = [UITableViewCell ][[textField superview] superview];  
  5. NSIndexPath *indexPath = [tableView indexPathForCell:cell];  
  6. }  


  1. - (void)textFieldDidEndEditing:(UITextField *)textField  
  2. {  
  3.     //get cell  
  4.     UITableViewCell *cell  = (UITableViewCell *)[[textField superview] superview];  
  5.     NSIndexPath *indexPath = [tableView indexPathForCell:cell];  
  6. }  

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


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

  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     NSInteger row = [indexPath row];  
  4.       
  5.     static NSString  *CellIdentifier = @"CellIdentifier";  
  6.       
  7.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
  8.       
  9.     if (cell == nil) {  
  10.         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];  
  11.         cell.selectionStyle = UITableViewCellSelectionStyleNone;  
  12.     }  
  13.       
  14.     cell.textLabel.text = [_passwordArray objectAtIndex:row];  
  15.       
  16.     CGRect textFieldRect = CGRectMake(0.0, 0.0f, 215.0f, 31.0f);  
  17.     UITextField *theTextField = [[UITextField alloc] initWithFrame:textFieldRect];  
  18.     theTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;  
  19.     theTextField.returnKeyType = UIReturnKeyDone;  
  20.     theTextField.secureTextEntry = YES;  
  21.     theTextField.clearButtonMode = YES;  
  22.     theTextField.tag = row;  
  23.     theTextField.delegate = self;  
  24.   
  25. //此方法为关键方法  
  26.     [theTextField addTarget:self action:@selector(textFieldWithText:) forControlEvents:UIControlEventEditingChanged];  
  27.       
  28.     switch (row) {  
  29.         case 0:  
  30.             theTextField.placeholder = @"请输入旧密码";  
  31.             break;  
  32.         case 1:  
  33.             theTextField.placeholder = @"请输入新密码";  
  34.             break;  
  35.         case 2:  
  36.             theTextField.placeholder = @"请再次输入新密码";  
  37.             break;  
  38.         default:  
  39.             break;  
  40.     }  
  41.       
  42.     cell.accessoryView = theTextField;   
  43.     [theTextField release];  
  44.       
  45.     return cell;  
  46. }  
  47.   
  48. - (void)textFieldWithText:(UITextField *)textField  
  49. {  
  50.     switch (textField.tag) {  
  51.         case 0:  
  52.             self.theOldPassword = textField.text;  
  53.             break;  
  54.         case 1:  
  55.             self.theNewPassword = textField.text;  
  56.             break;  
  57.         case 2:  
  58.             self.theTwiceNewPassword = textField.text;  
  59.             break;  
  60.         default:  
  61.             break;  
  62.     }  
  63. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值