Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextField in UITableViewCell Help

I have scoured the internet looking for a good tutorial or posting about having a UITableView populated with a UITextField in each cell for data entry.

I want to keep track of each UITextField and the text written within it while scrolling. The tableView will be sectioned. I have been using a custom UITableViewCell but I'm open to any method.

Also, is it possible to use the textFields as ivars?

If any of you could point me in the right direction, it would be greatly appreciated.

Thank you in advance!

like image 864
W Dyson Avatar asked Dec 31 '10 05:12

W Dyson


3 Answers

To solve your problem you have to maintain an array, with some number (number of textFields you added to all cells) of objects.

While creating that array you need add empty NSString objects to that array. And each time while loading the cell you have to replace the respected object to respected textField.

Check the following code.

- (void)viewDidLoad{
    textFieldValuesArray = [[NSMutableArray alloc]init];
    for(int i=0; i<numberofRows*numberofSections; i++){
        [textFieldValuesArray addObject:@""];
    }

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return numberofSections;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return numberofRows;
}

- (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:identifier];

     CustomTextField *tf = [[CustomTextField alloc] initWithFrame:CGRectMake(5,5,290,34)];
     tf.tag = 1;
     [cell.contentView addSubView:tf];
     [tf release];
    }
    CustomTextField *tf = (CustomTextField*)[cell viewWithTag:1];
    tf.index = numberofSections*indexPath.section+indexPath.row;
    tf.text = [textFieldValuesArray objectAtIndex:tf.index];

    return cell;
    }

- (void)textFieldDidEndEditing:(UITextField *)textField{

    int index = textField.index;
    [textFieldValuesArray replaceObjectAtIndex:index withObject:textField.text];
}
like image 169
Satya Avatar answered Sep 23 '22 21:09

Satya


First of all, you must understand that UITableViewCell and UITextField are just views, they are not supposed to hold data, they are just supposed to display them and allow the user to interact with them: The data should remain stored in the controller of the table view.

You have to remember that UITableView allows you to reuse UITableViewCell instances for performance purpose: what's displayed on the screen are actually the only subviews UITableView keep there. It means that you'll reuse one cell that already has a text field in it and set the text on that field directly. When the user will tap on the field it will edit it and you'll have to get the value back from it when the user will have finished.

The fastest way, would be to use what Satya proposes, that is building normal UITableViewCell and insert into a UITextField (there's no need for a CustomTextField class...). The tag will allow you to get back to the text field easily... But you'll have to setup your text field so it behaves properly when the table view resizes or if a label in the same cell changes.

The cleanest way to do that is to subclass UITableViewCell and setup the layout of your label and text field, and you can provide the text field as a property of the custom subclass.

like image 25
Psycho Avatar answered Sep 22 '22 21:09

Psycho


I have used Textfields in tableview for data entry. I have customised the UITextField class in a separate class called Utility :

In Utility.h

 @interface CustomUITextField:UITextField{
        NSInteger rowNumber;
    }

In Utility.m

@implementation CustomUITextField

@synthesize rowNumber;
@end

My tableView cellForRowAtIndexPath method is

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *Identifier = @"Cell";
    UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:Identifier];
    if(cell == nil)
        cell = [self reuseTableViewCellWithIdentifier:Identifier withIndexPath:indexPath];

    CustomUITextField *itemNameTextField = (CustomUITextField *)[cell.contentView viewWithTag:TEXTFIELD_TAG];//this is the tag I have set in reuseTableViewCellWithIdentifier method for textfield
    itemNameTextField.rowNumber = indexPath.row;
    itemNameTextField.text = @"";//you can set it for the value you want
    if(itemListTable.editing)
        itemNameTextField.borderStyle = UITextBorderStyleRoundedRect;
    else
        itemNameTextField.borderStyle = UITextBorderStyleNone;



    return cell;

}

You can customise the delegate methods of UITextField for CustomUITextField & can save the text entered in a particular row's textfield by accessing the CustomTextField's row number.

Just try with this.

like image 32
iPhoneDev Avatar answered Sep 23 '22 21:09

iPhoneDev