views:

518

answers:

1

Hi All,

After quite a long time perusing the web i have decided to come here as i usually have my questions answered super speedy and super well on Stack Overflow!

I am attempting to complete the following and would be grateful of any suggestions or pointers in the right direction:

  • Display a UITableView
  • When a "+" button is pressed a UITextField is added to a row of the UITableView
  • The user can type a string into this field
  • If the user selects the "+" Button again another UITextfield is added to the next row. (You get the idea)
  • When the user is done adding UITextFields and filling them in they can hit a "Save" button at which point all entries in each row are saved to an array.

Any suggestions regarding this would be greatly appreciated

Thanks!

Tom

A: 

This pseudocode might be a starting point:

// define an NSMutableArray called fields

- (IBAction)addField:(id)sender {
    // create a new text field here and add it to the array
    // then reload data
}

- (IBAction)save:(id)sender {
    for(UITextField *textField in fields) {
        // whatever you have to do
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // you have as many rows as textfields
    return [fields count];
}

- (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:CellIdentifier] autorelease];
    }
    // Set up the cell...
    // For row N, you add the textfield N to the array.
    [cell addSubview:(UITextField *)[fields objectAtIndex:indexPath.row]];
}
Daniel Hepper
Thanks guys.. i know that was a little vague ... but his has really helped and i am well on my way. Thank you
Tom G