- Adding a UITextField to a UITableViewCell only shows a textField in the LAST cell
- - (IBAction)editButton:(id)sender
- {
- if (self.editing)
- {
- [self setEditing:NO animated:YES];
- [self.myTableView setEditing:NO animated:YES];
- EditButton.title = @"Edit";
- cellText.hidden = YES; //<-- THIS IS THE CODE
- }
- else
- {
- [self setEditing:YES animated:YES];
- [self.myTableView setEditing:YES animated:YES];
- EditButton.title = @"Done";
- cellText.hidden = NO; //<-- THIS IS THE CODE
- }
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
- {
- static NSString *CellIdentifier = @"Cell";
- cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil)
- {
- cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
- cellText = [[UITextField alloc]init];
- [cellText setFrame:CGRectMake(190, 15, 55, 30)];
- cellText.text = @"1";
- cellText.borderStyle = UITextBorderStyleRoundedRect;
- cellText.hidden = YES;
- cellText.userInteractionEnabled = NO;
- [cell addSubview:cellText];
- }
- return cell;
- }
- - (IBAction)editButton:(id)sender
- {
- if (self.editing)
- {
- [self setEditing:NO animated:YES];
- [self.myTableView setEditing:NO animated:YES];
- EditButton.title = @"Edit";
- }
- else
- {
- [self setEditing:YES animated:YES];
- [self.myTableView setEditing:YES animated:YES];
- EditButton.title = @"Done";
- }
- [self.myTableView reloadData];
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
- {
- static NSString *CellIdentifier = @"Cell";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
- cellText = [[UITextField alloc]init];
- [cellText setFrame:CGRectMake(190, 15, 55, 30)];
- cellText.text = @"1";
- cellText.borderStyle = UITextBorderStyleRoundedRect;
- cellText.hidden = YES;
- cellText.backgroundColor = [UIColor redColor];
- cellText.userInteractionEnabled = NO;
- [cell addSubview:cellText];
- cellText.hidden=!self.editing;
- return cell;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.navigationItem.rightBarButtonItem = self.editButtonItem;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- static NSString *CellIdentifier = @"Cell";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil) {
- cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
- }
- UITextField * cellText = [[UITextField alloc] initWithFrame:CGRectMake(1, 1, 100, 30)];
- cellText.tag = 1;
- cellText.textColor = [UIColor darkTextColor];
- //cellText.numberOfLines = 0;
- cellText.font = [ UIFont fontWithName: @"Helvetica-Bold" size: 12.0 ] ;
- cellText.backgroundColor = [ UIColor clearColor ] ;
- cellText.text = @"123";
- cellText.hidden = YES;
- [cell.contentView addSubview:cellText];
- [cellText release];
- cellText =nil;
- // Set up the cell...
- return cell;
- }
- - (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
- // Detemine if it's in editing mode
- UITextField *cellText = (UITextField *)[[aTableView cellForRowAtIndexPath:indexPath] viewWithTag:1];
- if (!self.editing)
- {
- [self setEditing:NO animated:YES];
- [self.tableView setEditing:NO animated:YES];
- // EditButton.title = @"Edit";
- cellText.hidden = YES; //<-- THIS IS THE CODE
- }
- else
- {
- [self setEditing:YES animated:YES];
- [self.tableView setEditing:YES animated:YES];
- // EditButton.title = @"Done";
- cellText.hidden = NO; //<-- THIS IS THE CODE
- }
- return UITableViewCellEditingStyleNone;
- }