user4642233
user4642233

Reputation:

Get UItextFields text from custom UITableView

I have a custom UITableViewCell with a UITextField (which is linked to the custom cells class). I am trying to access the textField from my VC class.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    menuCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];

    if ([indexpath row] == 2) {
        menuCell.nameTextField.delegate = self;
    }

    return cell;
}

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

}

How do I get the textFields text from textFieldDidEndEditing?

Upvotes: 0

Views: 1834

Answers (2)

sam_smith
sam_smith

Reputation: 6093

Depending on where you want to access this text depends on how difficult it is.

  1. Want to access the text in cellForRowAtIndex - (very easy)

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        menuCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
    
        if ([indexpath row] == 2) {
            menuCell.nameTextField.delegate = self;
        }
    
        NSString * text = menuCell.nameTextField.text;
    
        return cell;
    
  2. If you want to access the text anywhere in the VC and the menuCell is unique (there is only one of them) - (medium difficult)

In your header file add the custom cell as a class

@class menuCell;

This means you can set it a variable in the interface

menuCell * _menuCell;

Next in cellForRowAtIndex you want to allocate this custom cell

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

    if (indexPath.section == bCustomCellSection) {

        if (!_menuCell) {

            _menuCell = [tableView_ dequeueReusableCellWithIdentifier:bProfileNameCell];
            _menuCell.nameTextField.delegate = self;
        }

        _menuCell.nameTextField.placeholder = @"Name";
        _menuCell.selectionStyle = UITableViewCellSelectionStyleNone;

        return _menuCell;
    }

...

}

This means that we now have access to the menu cell from anywhere in the VC and can get the text by calling

_menuCell.nameTextField.text
  1. Multiple custom cells with multiple textfields - (tough)

I have never done this but would probably do it one of two ways

a) Create an array and as we are creating the custom cells add a pointer to the textFields to the array each time. We can then access the textField we want from that array

For this method I would add the custom cells to a mutable array defined in the interface

NSMutableArray * cellsArray;

remember to initialise it in viewDidLoad

cellsArray = [NSMutableArray new];

Then in cellForRowAtIndex i would add the cell each time

menuCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
[cellsArray addObject: menuCell];

This obviously depends on how many sections we have. If we have more than one section it gets more complicated again:

Then we would need to add an array for each section to an overall array. This is quite complicated and could have a whole question on its own, there is a good link of how to do this here:

Once you have an array of cells (or an array of arrays of cells) you can call the cell you want based on the indexPath and get the textField

b) Call a pointer to the specific cell we want

menuCell * menuCell = [self tableView:table cellForRowAtIndexPath:indexPath];

and then get the textField from this cell as we did previously.

Remember you can calculate your own indexPath if you want to create one outside of cellForRow:

NSIndexPath * indexPath = [self.tableView indexPathForCell:cell];

This method is pretty good if you want to access a specific cell but a bit cumbersome if you want to access it a lot and keep having to call this code all over your VC

Hope this helps

Upvotes: 1

sdw
sdw

Reputation: 1

If you are asking how to get the text from the delegate method textFieldDidEndEditing, then you simply do this:

-(void) textFieldDidEndEditing:(UITextField*) textfield
{
   NSString *textFieldText = textfield.text; 
}

However, if you have multiple textFields and you want to know what textfield is calling the delegate, you could tag your textField:

[myTextField setTag:indexPath.row]

and then put a if statement in the delegate textFieldDidEndEditing like this:

-(void) textFieldDidEndEditing:(UITextField*) textfield
{
    if(textfield.tag == index0) do something..
    else if(textfield.tag == index1) do something..
}

Upvotes: 0

Related Questions