1

I'd like to create a screen similar to the "New Contact" screen of the iPhone Contacts app. There are little green '+' signs next to "add phone", "add email", etc. When the user clicks on these, new rows (or in the case of "add address", I suppose new sections) are created.

How can I create a similar behaviour in my Table View Controller?

Thanks, Daniel

2 Answers 2

1

here is an example how to add lines to a TableView:

// holding your data
NSMutableArray* data;

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

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

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    //if you want to add a section with one row:
    NSMutableArray *row = [[NSMutableArray alloc] init];
    [row addObject:@"Some Text"];
    [data addObject:row];
    [tableView reloadData];

    //if you want to add a row in the selected section:
    row = [data objectAtIndex:indexPath.section];
    [row addObject:@"Some Text"];
    [tableView reloadData];
}

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

    cell.textLabel.text = [[data objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

    return cell;
}

There should be a new row in your Tableview. Next step is to replace "Some Text" with our own data.

Sign up to request clarification or add additional context in comments.

Comments

0

This is the general approach I would take.

Create a custom tableview cell and create a delegate for it with something like

-(void)actionButtonTappedInTableViewCell:(UITableViewCell*)cell;

Make your view controller the delegate for the tableview cell and when that action gets triggered do the following:

-(void)actionButtonTappedInTableViewCell:(UITableViewCell*)cell
{
    NSIndexPath *oldIndexPath = [self.tableView indexPathForCell:cell];
    NSIndexPath *pathToInsert = [NSIndexPath indexPathForRow:(oldIndexPath.row + 1) inSection:oldIndexPath.section];

    [self.tableView beginUpdates];

    //now insert with whatever animation you'd like
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:pathToInsert] withRowAnimation:UITableViewRowAnimationAutomatic];


    [self.tableView endUpdates];
}

Add the index paths of your "special" rows to arrays and in your cellForRow method check if this is a special row and set it up as such.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.