我们希望添加一个UIextLabel来代替UITableViewCell的UITableViewCellStyleSubtitle的默认detailTextLabel。我可以显示UITextLabel,但是问题是我无法像对待detailTextLabel一样对齐它。我尝试过的以下代码是这样:
switch (indexPath.row) {
case 1:{
cell.accessoryType = UITableViewCellAccessoryNone;
cell.detailTextLabel.numberOfLines = 1;
//cell.detailTextLabel.text = @"Enter your display name";
//self.displayNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
self.displayNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(cell.detailTextLabel.frame.origin.x, cell.detailTextLabel.frame.origin.y, cell.detailTextLabel.frame.size.height, cell.detailTextLabel.frame.size.width)];
self.displayNameTextField.placeholder = @"Enter your display name";
self.displayNameTextField.autocorrectionType = UITextAutocorrectionTypeNo;
[self.displayNameTextField setClearButtonMode:UITextFieldViewModeWhileEditing];
[cell addSubview:self.displayNameTextField];
break;
}
default:
break;
}
答案 0 :(得分:0)
您的代码中的一个小更正。您可以使用自己的代码进行一些更改。
尝试一下。它将起作用。
switch (indexPath.row){
case 1:{
cell.accessoryType = UITableViewCellAccessoryNone;
cell.detailTextLabel.numberOfLines = 1;
UITextField *displayNameTextField = [[UITextField alloc] initWithFrame:cell.detailTextLabel.frame];
displayNameTextField.placeholder = @"Enter your display name";
displayNameTextField.autocorrectionType = UITextAutocorrectionTypeNo;
[displayNameTextField setClearButtonMode:UITextFieldViewModeWhileEditing];
[cell addSubview:displayNameTextField];
break;
}
default:
break;
}