添加到UITableViewCell的UITextField

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了添加到UITableViewCell的UITextField相关的知识,希望对你有一定的参考价值。

This adds a UITextField to a table view so that you can enter a value with the keyboard.
  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  2.  
  3. UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
  4. if (cell == nil) {
  5. cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
  6. reuseIdentifier:kCellIdentifier] autorelease];
  7. cell.accessoryType = UITableViewCellAccessoryNone;
  8.  
  9. if ([indexPath section] == 0) {
  10. UITextField *playerTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
  11. playerTextField.adjustsFontSizeToFitWidth = YES;
  12. playerTextField.textColor = [UIColor blackColor];
  13. if ([indexPath section] == 0) { // Email & Password Section
  14. if ([indexPath row] == 0) {
  15. playerTextField.placeholder = @"[email protected]";
  16. playerTextField.keyboardType = UIKeyboardTypeEmailAddress;
  17. playerTextField.returnKeyType = UIReturnKeyNext;
  18. }
  19. else {
  20. playerTextField.placeholder = @"Required";
  21. playerTextField.keyboardType = UIKeyboardTypeDefault;
  22. playerTextField.returnKeyType = UIReturnKeyDone;
  23. playerTextField.secureTextEntry = YES;
  24. }
  25. }
  26. playerTextField.backgroundColor = [UIColor whiteColor];
  27. playerTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
  28. playerTextField.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support
  29. playerTextField.textAlignment = UITextAlignmentLeft;
  30. playerTextField.tag = 0;
  31. //playerTextField.delegate = self;
  32.  
  33. playerTextField.clearButtonMode = UITextFieldViewModeNever; // no clear 'x' button to the right
  34. [playerTextField setEnabled: YES];
  35.  
  36. [cell addSubview:playerTextField];
  37.  
  38. [playerTextField release];
  39. }
  40. }
  41. if ([indexPath section] == 0) { // Email & Password Section
  42. if ([indexPath row] == 0) { // Email
  43. cell.textLabel.text = @"Email";
  44. }
  45. else {
  46. cell.textLabel.text = @"Passwort";
  47. }
  48. }
  49. else { // Login button section
  50. cell.textLabel.text = @"Log in";
  51. }
  52. return cell;
  53. }

以上是关于添加到UITableViewCell的UITextField的主要内容,如果未能解决你的问题,请参考以下文章

如何将 UIViewController/UITabBar 添加到 UITableViewCell?

将按钮添加到 UITableViewCell

如何将目标操作添加到 UITableViewCell

将大于 cellHeight 的子视图添加到 UITableViewCell?

将 UIImageView 添加到自定义 UITableViewCell

防止 UIView 被重新添加到 UITableViewCell