如果我将文本字段放在cell.contentview上,我如何在textfield上返回键盘,我使用的是textfieldshouldreturn方法,但它不能正常工作所以请告诉我如何解决它? 这是我在tableview单元格上设置文本字段的代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellidentitifier = @"cell";
UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentitifier];
if (cell ==nil)
{
cell = [tableView dequeueReusableCellWithIdentifier:cellidentitifier];
}
else
{
[cell prepareForReuse];
}
UIImageView *imgview = [[UIImageView alloc]initWithFrame:CGRectMake(30, 10, 70, 70)];
imgview.backgroundColor = [UIColor blackColor];
[cell.contentView addSubview:imgview];
UITextField *_nameTF = [[UITextField alloc]initWithFrame:CGRectMake(120, 10, 160, 22)];
_nameTF.placeholder =@"Client Name";
_nameTF.layer.borderWidth = 1.0;
[cell.contentView addSubview:_nameTF];
_nameTF = nil;
UITextField *_timeTF = [[UITextField alloc]initWithFrame:CGRectMake(120, 37, 160, 22)];
_timeTF.placeholder = @"Time";
_timeTF.layer.borderWidth= 1.0;
[cell.contentView addSubview:_timeTF];
_timeTF = nil;
UITextField *_PhoneTF = [[UITextField alloc]initWithFrame:CGRectMake(120, 64, 160, 22)];
_PhoneTF.placeholder = @"Phone";
_PhoneTF.layer.borderWidth= 1.0;
[cell.contentView addSubview:_PhoneTF];
_PhoneTF = nil;
return cell;
}
答案 0 :(得分:1)
在return cell
语句之前将委托设置为cellForRowAtIndexPath中的textfield,如下所示。
_nameTF.delegate = self;
_timeTF.delegate = self;
_PhoneTF.delegate = self;
和
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
以及.h文件中的viewcontroller
设置代理
@interface myviewVC : UIViewController<UITextFieldDelegate>