1

I just checked the datasource and the UITableView of the app and they seem to be working well. I can add a row to the TableView by using the code - [...initWithObjects:(id),nil] and I could do this at code level.

This is what I want to do.

  1. Create an Add button or a '+' sign on top of the table.
  2. So while the App is running.. If I click that, I should be able to set a name for that row.
  3. If I need to create more rows and set names for them, I just press the add button and I again type new names for the rows .

How do I go about this in real time?

Legolas
  • 12,145
  • 12
  • 79
  • 132
  • I got the answer here. http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/TableView_iPhone/ManageInsertDeleteRow/ManageInsertDeleteRow.html – Legolas May 26 '11 at 20:32
  • http://www.iphonedevsdk.com/forum/iphone-sdk-tutorials/3481-uitableview-tutorial-part-2-a.html – Legolas May 26 '11 at 21:59

2 Answers2

5

The UITableView method -insertRowsAtIndexPaths:withRowAnimation: is used to add rows to a table programmatically.

This answer addresses your problem fairly directly.

Community
  • 1
  • 1
Seamus Campbell
  • 17,816
  • 3
  • 52
  • 60
1

It's fairly straightforward... and this is from memory, so I might have a typo here or there. The idea should work though.

In tableView:numberOfRowsInSection: you give return [myArray count] + 1; (you're adding one row to the total)

In tableView:cellForRowAtIndexPath: the cell where [indexPath row] == [myArray count] is where make a cell with "Add Row" text, rather than whatever your data source is, because it goes to [myArray count]-1. (I think that makes sense). for example,

    if([indexPath row] == [myArray count]){
        cell.textLabel.text = @"Add New Item";
    } else {
         cell.textLabel.text = [[myArray objectAtIndex:[indexPath row]]
                                           valueForKey:@"title"];            
    }
    return cell;

Then you would insert the new record into your myArray and the table.

As for actually changing the new row details, you have some options of creating a custom cell with some kind of textual input or using a modal view controller with a prompt. It's really up to you.

Cameron
  • 1,142
  • 8
  • 32
  • Thanks, I had been adding an extra row and making the mistake of [indexPath row] > [myArray count] – rob123 Feb 05 '17 at 23:04