Question

I am trying for many days now to set and get inputs of UITextFields in cells of sections.

In my first view i have go a UITextField where I say how many sections I want to have, and when pressing a button, it gives me that sections (with 4 rows) in the UITableView. my problem now is, that I don't get the correct values of each UITextField.

for example:

  • all sections have 4 textfields
  • in the textfields in can write int or float
  • when i know the int from textfield 1 and 2, i divide them...

here is my code....

#import "ViewController.h"

@interface ViewController () 
{
    NSMutableArray *sectionsArray;
    NSMutableArray *rowsArray;
    NSMutableArray *textFieldArray;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.

    [[self myTableView]setDelegate:self];
    [[self myTableView]setDataSource:self];

    sectionsArray = [[NSMutableArray alloc]init];
    rowsArray = [[NSMutableArray alloc]initWithObjects:@"",@"",@"",@"", nil];
    textFieldArray = [[NSMutableArray alloc]initWithObjects:@"",@"",@"",@"", nil];

}

- (void)viewWillAppear:(BOOL)animated 
{
    [super viewWillAppear:animated];
    [self.myTableView reloadData]; 
}


- (void)didReceiveMemoryWarning 
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated. 
}

#pragma mark - Table View

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    int anzahl = [_AnzahlStationen intValue];//from first view...anzahl=quantity of sections

    //adds sections
    do {

        [sectionsArray addObject:[NSString stringWithFormat:@"Palette %lu",sectionsArray.count+1]];

    } while ([sectionsArray count] < anzahl);

    return sectionsArray.count;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return rowsArray.count;
}


-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{
    return  [sectionsArray objectAtIndex:section];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell==nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 300, 24)];
        textField.font = [UIFont fontWithName:@"Gujarati Sangam MN" size:15];
        textField.textColor = [UIColor redColor];

        textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        textField.tag = 17;
        [cell.contentView addSubview:textField];
        textField.delegate = self;
    }

    UITextField *textField = (UITextField *)[cell.contentView viewWithTag:17];

    textField.text = [textFieldArray objectAtIndex:indexPath.row];

    return cell; 
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES; 
}

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

    [textFieldArray replaceObjectAtIndex:0 withObject:textField.text];
}

@end

How can I replace the correct UITextField in the correct row?

Was it helpful?

Solution

I recommend you use a datasource similar to this:

NSMutableArray *tableViewData = [NSMutableArray array];

[tableViewData addObject:@[ @{@"textfield" : YourTextField} ]];
[tableViewData addObject:@[ @{@"textfield" : YourTextField} ]];
[tableViewData addObject:@[ @{@"textfield" : YourTextField} ]];
[tableViewData addObject:@[ @{@"textfield" : YourTextField} ]];

The above code represents tableViewData, containing 4 arrays, each array contains one dictionary which will maintain the data for a single cell. In my example I have just used a single key-value pair of a UITextField.

You can dynamically create this however you like, and your delegate and datasource methods can read from it. Rather than having 3 separate arrays.

E.g.

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
     return [tableViewData count];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[tableViewData objectAtIndex:section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    NSDictionary *cellData = [[tableViewData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

    // Create your cell and use the data stored in the dictionary
}

By using the above approach, you are able to update this dictionary, and maintain a state for all of your UITableView in a single place, and it keeps the code tidy and readable. To access any UITextField, just find it in the dictionary in the UITableView datasource and delegate methods.

Hope this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top