在自定义UITableViewCell中设置UITextField的委托

时间:2014-12-05 16:05:10

标签: uitableview delegates

我使用的是标准版UIViewController,并在UITableView方法中添加了ViewDidLoad。像这样:

- (void)viewDidLoad {

_settingsTable =  [[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y+80, self.view.frame.size.width, self.view.frame.size.height / 2) style:UITableViewStylePlain];
[_settingsTable setShowsHorizontalScrollIndicator:NO];
[_settingsTable setShowsVerticalScrollIndicator:NO];
_settingsTable.separatorStyle = UITableViewCellSeparatorStyleNone;
_settingsTable.backgroundColor = [UIColor clearColor];
[self.view addSubview:_settingsTable];

_settingsTable.dataSource = self;
_settingsTable.delegate = self;

_player01Cell = [[UITableViewCell alloc] init];
_player01Cell.backgroundColor = [UIColor clearColor];
_player01Cell.textLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Left player", nil) ];
_player01Cell.textLabel.font = [UIFont fontWithName:@"AlfaSlabOne-Regular" size:12];
_player01Cell.textLabel.layer.shadowOpacity = 1.0;
_player01Cell.textLabel.layer.shadowRadius = 1.0;
_player01Cell.textLabel.layer.shadowColor = [UIColor blackColor].CGColor;
_player01Cell.textLabel.layer.shadowOffset = CGSizeMake(0.0f, 2.0f);
_player01Cell.textLabel.textColor = [UIColor whiteColor];
_player01Cell.textLabel.textAlignment = NSTextAlignmentLeft;

_aiPlayer01TextField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, _player01Cell.contentView.bounds.size.width-135, 30)];
_aiPlayer01TextField.center = CGPointMake(130+_aiPlayer01TextField.frame.size.width/2, _player01Cell.contentView.bounds.size.height/2);
_aiPlayer01TextField .placeholder = [NSString stringWithFormat:NSLocalizedString(@"Leftplayer", nil)];
_aiPlayer01TextField.backgroundColor = [UIColor clearColor];
_aiPlayer01TextField.clearButtonMode = UITextFieldViewModeAlways;
_aiPlayer01TextField.font = [UIFont fontWithName:@"AlfaSlabOne-Regular" size:15];
_aiPlayer01TextField.layer.shadowOpacity = 1.0;
_aiPlayer01TextField.layer.shadowRadius = 1.0;
_aiPlayer01TextField.layer.shadowColor = [UIColor blackColor].CGColor;
_aiPlayer01TextField.returnKeyType = UIReturnKeyDone;
_aiPlayer01TextField.layer.shadowOffset = CGSizeMake(0.0f, 2.0f);
_aiPlayer01TextField.textColor = [UIColor whiteColor];
_aiPlayer01TextField.textAlignment = NSTextAlignmentLeft;
[_aiPlayer01TextField resignFirstResponder];
[_player01Cell addSubview:_aiPlayer01TextField];

[super viewDidLoad];
}

除了UITextField

的代表之外,一切都按预期工作

我可以触摸文本字段并弹出键盘并正常工作,但按完成后它不会消失。触摸视图后它也不会消失。我之前有过工作,但是文本字段只是放在主视图上,这是<UITextFieldDelegate>但现在,当我在自定义单元格中有文本字段时,委托似乎不起作用。

我在代码中也有这个:

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[_aiPlayer01TextField resignFirstResponder];
[_aiPlayer02TextField resignFirstResponder];
[_aiPlayer03TextField resignFirstResponder];
return YES;
}

-(void)textFieldReturn:(UITextField *)textField
{
[_aiPlayer01TextField resignFirstResponder];
[_aiPlayer02TextField resignFirstResponder];
[_aiPlayer03TextField resignFirstResponder];
return;
}


- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"TOUCH");
[[self view] endEditing:YES];
}

如何将单元格设置为文本字段的委托?或者我应该将tableView设置为委托吗?

非常感谢!

0 个答案:

没有答案