Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 4.06 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Adding a UITextField to a UITableViewCell only shows a textField in the LAST cell
  2. - (IBAction)editButton:(id)sender
  3. {
  4.     if (self.editing)
  5.     {
  6.         [self setEditing:NO animated:YES];
  7.         [self.myTableView setEditing:NO animated:YES];
  8.         EditButton.title = @"Edit";
  9.         cellText.hidden = YES;  //<-- THIS IS THE CODE
  10.     }
  11.     else
  12.     {
  13.         [self setEditing:YES animated:YES];
  14.         [self.myTableView setEditing:YES animated:YES];
  15.         EditButton.title = @"Done";
  16.         cellText.hidden = NO;  //<-- THIS IS THE CODE
  17.     }
  18. }
  19.        
  20. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
  21. {
  22.     static NSString *CellIdentifier = @"Cell";
  23.  
  24.     cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  25.     if (cell == nil)
  26.      {
  27.          cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  28.  
  29.  
  30.          cellText = [[UITextField alloc]init];
  31.          [cellText setFrame:CGRectMake(190, 15, 55, 30)];
  32.          cellText.text = @"1";
  33.          cellText.borderStyle = UITextBorderStyleRoundedRect;
  34.          cellText.hidden = YES;
  35.          cellText.userInteractionEnabled = NO;
  36.          [cell addSubview:cellText];
  37.     }    
  38.  
  39. return cell;
  40. }
  41.        
  42. - (IBAction)editButton:(id)sender
  43. {
  44.  if (self.editing)
  45.         {
  46.             [self setEditing:NO animated:YES];
  47.             [self.myTableView setEditing:NO animated:YES];
  48.             EditButton.title = @"Edit";
  49.  
  50.         }
  51.         else
  52.         {
  53.             [self setEditing:YES animated:YES];
  54.             [self.myTableView setEditing:YES animated:YES];
  55.             EditButton.title = @"Done";
  56.  
  57.         }
  58.         [self.myTableView reloadData];
  59. }
  60.        
  61. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
  62. {
  63.     static NSString *CellIdentifier = @"Cell";
  64.  
  65.     UITableViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  66.     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
  67.  
  68.  
  69.     cellText = [[UITextField alloc]init];
  70.     [cellText setFrame:CGRectMake(190, 15, 55, 30)];
  71.     cellText.text = @"1";
  72.     cellText.borderStyle = UITextBorderStyleRoundedRect;
  73.     cellText.hidden = YES;
  74.     cellText.backgroundColor = [UIColor redColor];
  75.     cellText.userInteractionEnabled = NO;
  76.     [cell addSubview:cellText];
  77.  
  78.     cellText.hidden=!self.editing;
  79.  
  80.     return cell;
  81. }
  82.        
  83. - (void)viewDidLoad {
  84.     [super viewDidLoad];
  85.     self.navigationItem.rightBarButtonItem = self.editButtonItem;
  86. }
  87.  
  88.  
  89.  
  90. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  91.  
  92. static NSString *CellIdentifier = @"Cell";
  93.  
  94. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  95. if (cell == nil) {
  96.     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
  97. }
  98. UITextField * cellText = [[UITextField alloc] initWithFrame:CGRectMake(1, 1, 100, 30)];
  99. cellText.tag = 1;
  100. cellText.textColor = [UIColor darkTextColor];
  101. //cellText.numberOfLines = 0;
  102. cellText.font = [ UIFont fontWithName: @"Helvetica-Bold" size: 12.0 ] ;
  103. cellText.backgroundColor = [ UIColor clearColor ] ;
  104. cellText.text = @"123";
  105.     cellText.hidden = YES;
  106. [cell.contentView addSubview:cellText];
  107. [cellText release];
  108. cellText =nil;
  109.  
  110. // Set up the cell...
  111.  
  112.  
  113. return cell;
  114. }
  115. - (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  116. // Detemine if it's in editing mode
  117.   UITextField *cellText = (UITextField *)[[aTableView cellForRowAtIndexPath:indexPath] viewWithTag:1];
  118. if (!self.editing)
  119. {
  120.     [self setEditing:NO animated:YES];
  121.     [self.tableView setEditing:NO animated:YES];
  122.    // EditButton.title = @"Edit";
  123.  
  124.     cellText.hidden = YES;  //<-- THIS IS THE CODE
  125. }
  126. else
  127. {
  128.     [self setEditing:YES animated:YES];
  129.     [self.tableView setEditing:YES animated:YES];
  130.   //  EditButton.title = @"Done";
  131.     cellText.hidden = NO;  //<-- THIS IS THE CODE
  132. }
  133.  
  134. return UITableViewCellEditingStyleNone;
  135. }