Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In-place editing of text in UITableViewCell?

Tags:

iphone

I know this has to be a really simple question, but I am having trouble finding it in a simple place where there aren't so many other things going on that I can isolate the specific behavior.

I know I need to add a UITextField as a subview to the UITableViewCell to make it editable. What is the best way to do this? (I see an example in UICatalog, but I am confused with all of the extra things it is doing with the Dictionary.)

Again, please excuse the ignorance of this question. A pointer to sample code or a screencast would be much appreciated.

Thanks in advance,

Alan

like image 593
Alan Avatar asked Nov 08 '10 12:11

Alan


2 Answers

First, define about UITextField.

txtField=[[UITextField alloc]initWithFrame:CGRectMake(5, 5, 320, 39)];
txtField.autoresizingMask=UIViewAutoresizingFlexibleHeight;
txtField.autoresizesSubviews=YES;
txtField.layer.cornerRadius=10.0;
[txtField setBorderStyle:UITextBorderStyleRoundedRect];
[txtField setPlaceholder:@"Type Data Here"];

Then write the code below in cellForRowAtIndexPath method

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *CellIdentifier = @"CellIdentifier";
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   if (cell == nil)
   {
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
      cell.accessoryType = UITableViewCellAccessoryNone;
    }
    [cell addSubview:txtField];
    return cell;
}
like image 106
iphony Avatar answered Nov 15 '22 20:11

iphony


You can create your custom UITableViewCell and put whatever you want in it. Then you simply have to use it in your UITableView. If you are worried about what to use as UITextField delegate for your textfield to manage all the delegate methods, IMHO I'll put it in the UIViewController that handle the UITableView, so that you can update the DataSource once that the textfield has been edited.

like image 23
pasine Avatar answered Nov 15 '22 18:11

pasine