How do I make table view cells editable?

Discussion in 'iOS Programming' started by Aayush, Jun 10, 2010.

  1. Aayush macrumors newbie

    Aayush

    Joined:
    Nov 8, 2008
    Location:
    Siliguri, W.B., India
    #1
    Hello.

    I’m a beginner at programming for Apple’s platforms and have only recently managed to wrap my head around table views and view controllers on the iPhone.

    I’m trying to make a replica of the Contacts application (for practice) and am stuck at the point where you are required to enter your first, last and company names into three table cells. How do I make those table cells disallow selection and accept keyboard input?

    Any help would be appreciated. Thank you.
     
  2. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
  3. Aayush thread starter macrumors newbie

    Aayush

    Joined:
    Nov 8, 2008
    Location:
    Siliguri, W.B., India
    #3
    Thanks for the replies.

    It’s a default tableview that is controlled by a UITableViewController subclass. I know how to set the text for any of the cells:

    Code:
    cell.textLabel.text = @"Text goes here";
    But how do I allow the user to enter text there? You guys say that those cells contains text fields. How do I allow the user to edit them? Anywhere the word “edit” is mentioned in connection with the table view in the documentation, it only talks about deletion, insertion and reordering of rows (all of which I’m able to implement without any issues).
     
  4. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    A standard table with standard cells doesn't have the ability for users to edit the text in the cells. Yes, you can add, remove, and reorder the cells. If you want the user to be able to edit the text in a cell then you need to add a textfield or textview to the cell.

    You need to build custom cells.
     
  5. Aayush thread starter macrumors newbie

    Aayush

    Joined:
    Nov 8, 2008
    Location:
    Siliguri, W.B., India
    #5
    Ah, so that’s what I was missing. I’ve spent the past two days going through various pages of the documentation trying to figure it out. Even the Table View programming guide does not mention this. Thank you so much! :)

    Now I gotta figure out how to add a text field to a table view cell. This ought to be fun.
     
  6. AbhishekApple macrumors member

    Joined:
    Aug 5, 2010
    #6
    Hi ayush did u get the solution on this.....

    Hi phoneydeveloper

    Please suggest some better idea on following issue...

    I have added a textfield in the cell (not created customcell)and fetched the values from the textfield while editing the record...
    but the problem is
    1) the textfield is not getting deallocated even if i release the textfield memory as the cell retains the instance of textfield.....

    2) and also due to tableview property it creates the cell when cell is visible so continues alloction of textfield is their..

    3)I am inserting & deleting cell ( UITableViewCellEditingStyle).. so if i delete the cell the cell gets deallocated & the textfield vanishes but it is still their in the view(doesn't gets deallocated)

    Their is no crash while executing this code (following code is edited)
    Code:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *MyIdentifier = @"MyIdentifier";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    	if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
    		[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    	}
    	
    	CGRect bounds = [cell.contentView bounds];
    	CGRect rect = CGRectInset(bounds, 20.0, 10.0);
    	
    	switch (indexPath.section) {
    		case DContactSection:
    			NSLog(@"Conatcats child phone....");
    			if ([arryCPerson count] > 0) {
    				NSString *tag = [NSString stringWithFormat:@"%d%d%d",DContactSection+1,indexPath.row+1,3];
    				int _tag = [tag intValue];
    				NSString *_email = [[arryCPerson objectAtIndex:indexPath.row] objectForKey:@"scfname"];
    				UITextField *txt = [self setTextField:rect setTag:_tag];
    				txt.text = _email;
    				txt.placeholder=@"ContactPerson";
    				[cell.contentView addSubview:txt];
    			}
    			break;
    		case DCallSection:{
    			NSString *tag = [NSString stringWithFormat:@"%d%d%d",DCallSection+1,indexPath.row+1,3];
    			int _tag = [tag intValue];
    			UITextField *txt = [self setTextField:rect setTag:_tag];
    			txt.text = [mutdict objectForKey:@"tollfree"];
    			txt.placeholder=@"Tollfree";
    			[cell.contentView addSubview:txt];
    			break;
    		}
    			
    	}
        return cell;
    }
    
    -(UITextField*)setTextField:(CGRect)rect setTag:(int)_tag{
    	UITextField *txtcell = [[[UITextField alloc] init]autorelease];
    	rect.size.width=240;
    	txtcell.frame = rect;
    	txtcell.tag = _tag;
    	[txtcell setReturnKeyType:UIReturnKeyNext];
    	[txtcell setDelegate:self];
    	[txtcell setBackgroundColor:[UIColor whiteColor]];
    	[txtcell setOpaque:YES];	
    	return txtcell;
    }
    
    Also i have saved the textfield value on - (void)textFieldDidEndEditing:(UITextField *)textField.....
    but if i am on last textfield and do not move the cursor to other textfield and click the save button on navigation bar the above function is not called thus doesn't saves the last textfield value

    Code:
    - (void)textFieldDidEndEditing:(UITextField *)textField
    {
    	NSString *_tag = [NSString stringWithFormat:@"%d",[textField tag]]; 
    	NSRange range = NSMakeRange(0,1);
    	NSString *_sec = [_tag substringWithRange:range];
    	range = NSMakeRange(1,1);
    	NSString *_row = [_tag substringWithRange:range];
    	int sec =[_sec intValue]-1;
    	int row =[_row intValue]-1;
    	NSLog(@"sec= %d row=%d",sec,row);
    	NSLog(@"Textfield=%@",textField.text);
    
    	if (sec==DContactSection) {
    		[[arryCPerson objectAtIndex:row] setValue:textField.text forKey:@"scfname"];
    	}
    	if (sec==DCallSection) {
    		[mutdict setValue:textField.text forKey:@"tollfree"];
    	}
    }
     
  7. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #7
    I haven't implemented this myself due to not really needing it in my apps and my expectation that it will be complicated. So these comments are a little speculative.

    One key to implementing this is to have a separate data model that holds the text for each row and to update this data model on every change to the textField. This means every keydown and paste etc. needs to result in a change to your data model. The reason for this is that table view cells can be recycled if the user scrolls them offscreen. You've already discovered that you can't reliably save the text under some conditions. Doing what I suggest will fix that problem. It seems inefficient and a lot of overhead to save a new text string for every keydown. Doesn't matter. Do it that way.

    Regarding the code you show there are a couple comments. You are creating a new textField every time that cellForRowAtIndexPath is called. This is wrong. You should only create the new textField one time when the cell is created.

    You're using underscores for your variable names in an odd way. I recommend that you don't use them at all as a prefix for your variable names. You might conflict with an Apple ivar if you do. At any rate, if you're going to use them then use them the same way that Apple does. Don't just make up your own conventions.
     
  8. AbhishekApple macrumors member

    Joined:
    Aug 5, 2010
    #8
    Thanks phoneyDeveloper i will implement ur suggestion..
     

Share This Page