0

I'm new to Iphone app dev. I struck up with a problem. I'm having the table view in that i inserted textfield in each row. I done the UI Part. But how can i get the values from the textfield in tableview. I created customCell class . I could not use IBOutlet and all.

here is my code:

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

    static NSString *CellIdentifier = @"Cell";

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...

    return cell;
}
Nick Weaver
  • 47,228
  • 12
  • 98
  • 108
praveen
  • 1
  • 1
  • Thanks for u r replies. But my requirement is i want to enter the data in those text fields and i need to save that data in Database or Array. First i want to store in NSMutableArray. Then after i will try with Database. Thanks in Advance... – Praveen Apr 27 '11 at 13:03
  • @Praveen Got solution? – Siva Oct 24 '13 at 08:41
  • https://stackoverflow.com/questions/28431086/getting-data-from-each-uitableview-cells-swift See this link – kunal pal Oct 20 '17 at 18:14

2 Answers2

0

You should have a dataSource (usually a NSMutableArray) that (usually) contains the values for each cell. You should be able to get the value you need by using the indexPath, e.g.

NSObject *object = [dataArray objectAtIndex:indexPath.row];

In the above code I assume the dataSource for the tableView is an array called dataArray. I also assume it contains objects of the type NSObject (this usually isn't true in real world examples, in such situations it's generally a subclass like NSDictionary or a custom NSObject subclass.

Make sure the tableView is connected to it's dataSource. The connection is done by (at minimum) the methods setDataSource: on your UITableView instance and numberOfSectionsInTableView: and numberOfRowsInSection: delegate methods.

Your UITableViewCell subclass should generally not be used to save data in, if you use it in this way, you're taking the wrong approach. The following site should be a decent introduction in using the UITableView class: http://www.mobisoftinfotech.com/blog/iphone/introduction-to-table-view/

Wolfgang Schreurs
  • 11,779
  • 7
  • 51
  • 92
  • I think you misunderstood his problem. He has text fields in his custom cells and he wants to know what the user entered in them. – Erik B Apr 26 '11 at 14:33
  • If that is correct, he should probably create a delegate protocol for his **UITableViewCell** subclass (or just make the class with the tableView the delegate of the textField in the cell, not sure if this will work 100% correct). Make the class that contains the tableView a delegate of each UITableViewCell so that this class can handle the user input. – Wolfgang Schreurs Apr 26 '11 at 14:43
0

As Schreurs already explained you need to implement the UITextFieldDelegate protocol to your viewController (along with UITableViewDataSource), look into those in the documentation to learn more of what you can do with them. But this is more tricky than having distinct UITextFields in your view.

You have to consider the fact that when a cell leaves the visible range of the tableview it will be released or reused. So if for example a cell 1 contains a textfield your write something in it and then scroll to cell 15, you're gonna probably get a cell with the textfield of cell 1 and its contents. If you prepare your cells for reuse, emptying the textFields you have to keep that data somewhere to re-enter it in the proper cell. After all that you're gonna scratch your head what textField is calling your delegate (probably your viewController, so you're gonna have to tag them with a number from which you can extract a row number - i.e. cell.textField.tag = indexPath.row + 100).

So to sum up you want something like this in your viewController

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if ([textField.text length] > 0) {
        NSUInteger row = textField.tag - 1;
        [textFieldValues setObject:textField.text forKey:[NSNumber numberWithInt:row]];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId = @"cellId";
    TextFieldTableViewCell *cell = (TextFieldTableViewCell *) [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell)
        cell = [[[TextFieldTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId] autorelease];

    cell.textField.tag = indexPath.row + 1;
    cell.textField.delegate = self;
    NSString *value = [textFieldValues objectForKey:[NSNumber numberWithInt:indexPath.row]];
    if (value)
        cell.textField.text = value;
    else
        cell.textField.text = @"";

    return cell;
}

and then in your TextFieldTableViewCell.h

@property (nonatomic, readonly) UITextField *textField;

and finally in your TextFieldTableViewCell.m

@synthesize textField;

p.s. I was wandering what might happen when the editing textField leaves the visible cell range, and it isn't reused or released... gave me the chills! So didEndEditing should be adequate.

tsakoyan
  • 1,891
  • 14
  • 12