Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Editing TextLabel in a UITableViewCell [duplicate]

Possible Duplicate:
How to do edit-in-place in a UITableView?

I am new to this iphone developing. I just want to know how to edit/update UITextLabel in a tableView.

I am already using edit/done animation and able to delete rows but i am not able to get how to edit the text in those rows.

I want the user to edit the textlabel of the cell.when edit button is tapped.

i already searched the site but couldnt get the exact answer.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
if(!cell)
{
    cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"]autorelease];
    cell.editingAccessoryType=YES;
}


myTextField = [[UITextField alloc] initWithFrame:CGRectMake(15, 6, 241, 31)];
myTextField.backgroundColor = [UIColor clearColor];
myTextField.textAlignment = UITextAlignmentLeft;
myTextField.returnKeyType = UIReturnKeyDone;
myTextField.delegate = self;
cell.accessoryView = myTextField;
myTextField.text = [self.arrayOfItems objectAtIndex:indexPath.row];
myTextField.enabled = NO;
return cell;
}

-(IBAction)Edit:(id)sender
{
if(self.editing)
{
            [myTextField.Enabled = YES];
    [super setEditing:NO animated:NO];
    [self.tab setEditing:NO animated:NO];
    [self.tab reloadData];
    [self.navigationItem.rightBarButtonItem setTitle:@"Done"];
    [self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStylePlain];
}
else 
{
    myTextField.enabled= YES;

    [super setEditing:YES animated:YES];
    [self.tab setEditing:YES animated:YES];
    [self.tab reloadData];
    [self.navigationItem.rightBarButtonItem setTitle:@"Edit"];
    [self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone];
 }
 }

- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
{
if (self.editing == NO || !indexPath) 
return UITableViewCellEditingStyleNone;
if (self.editing && indexPath.row == ([self.arrayOfItems count])) 
{
    return UITableViewCellEditingStyleInsert;
} 
else 
{
    return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleNone;
}

This is not working for me.textfield is disappearing if i hit edit button

like image 227
chandu nhyt Avatar asked Nov 27 '22 11:11

chandu nhyt


2 Answers

Editing text in the table rows themselves is difficult because you need to manage the keyboard and scrolling the editable field up to make room for the keyboard etc. (that could be scrolled off screen while being edited etc.). Also if you make every cell editable you have to managed an edited value being able to be scrolled off screen mid-edit etc. (i.e. while the keyboard is active and so forth).

A UILabel is not editable, you need a UITextField to edit text.

The best way to achieve an edit of a value in a table cell is probably to push a new view controller onto the stack on the edit action and have an editable text field there and add save/cancel bar button items to the menu of that view controller.

When the user presses save, update the model behind the table view with the appropriate text value and pop the view controller off the stack again. Have the main view containing the table call reloadData on the tableView in its viewWillAppear method.

This will give you easiest/best control of the keyboard behaviour and editing behaviour when the fields are being edited.

like image 121
gamozzii Avatar answered Dec 01 '22 01:12

gamozzii


Use UITextField in place of UILabel in Your Custom cell,then You can edit.for custom cell You can check this Tutorial or You can Check this Apple Sample also.

like image 39
Mudit Bajpai Avatar answered Nov 30 '22 23:11

Mudit Bajpai