Posted By

suibhne on 11/10/10


Tagged

iphone objectivec UITableView


Versions (?)

UITextField added to a UITableViewCell


 / Published in: Objective C
 

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. }

Report this snippet  

You need to login to post a comment.