8

I'm new in objective-c. I have a tableView with 3 rows and 1 section. Can you help me how to add row with some text in a table by pressing button (addCity)?

- (void)viewDidLoad {
    [super viewDidLoad];
    m_bg.image = [UIImage imageNamed:[WeatherAppDelegate isNight]  ? @"SettingsBackNight.png" : @"SettingsBackDay.png" ];
    tableView.layer.cornerRadius = 10;
    m_scrollView.layer.cornerRadius = 10;
    [m_scrollView setContentSize:CGSizeMake(tableView.frame.size.width, tableView.frame.size.height)];
    dataArray = [[NSMutableArray alloc] initWithObjects:@"Moscow", @"London", @"Paris", nil];

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


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


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

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
    }

    [cell.textLabel setText: [dataArray objectAtIndex:indexPath.row]];
    return cell;
}


-(IBAction)addCity:(id)sender
{
    [dataArray addObject:@"City"];
    NSArray *paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:[dataArray count]-1 inSection:1]];
    [[self tableView] insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationTop];
    [tableView reloadData];
}
Sveta
  • 1,270
  • 3
  • 16
  • 33

2 Answers2

15

Your table must take its data from some source where you can add elements when needed. (say, NSMutableArray instance in your data source), then your methods will look like. For simplicity assume that you have dataArray containing NSStrings:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
   return 1;
}

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


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *MyIdentifier = @"MyIdentifier";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
  if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
  }
  [cell.textLabel setText: [dataArray objectAtIndex:indexPath.row];
  return cell;
}


-(IBAction)addCity:(id)sender
{
  [tableView beginUpdates];
  [dataArray addObject:@"City"];
  NSArray *paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:[dataArray count]-1 inSection:1]];
  [[self tableView] insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationTop];
  [tableView endUpdates];
}
Vladimir
  • 170,431
  • 36
  • 387
  • 313
  • Something like that? dataArray = [[NSMutableArray alloc] initWithObjects:@"Moscow", @"London", @"Paris", nil]; My program is crashed. – Sveta Mar 25 '11 at 08:26
  • @Sveta, yes array should be created this way... do you still have crash? – Vladimir Mar 25 '11 at 08:57
  • No, I'm removed insertRowsAtIndexPaths and my program is working! – Sveta Mar 25 '11 at 09:00
  • Then you probably did some `reloadData` inside the `addObject:` method of your dataArray. Because Vladimir is right, it should be done this way, with `insertRowsAtIndexPaths:`. – Cœur Aug 01 '13 at 13:50
  • insertRow code is working fine on button tap but not working on viewDidLoad method – DJtiwari May 09 '16 at 08:51
2

You don't insert rows into the tableView directly. You adopted the UITableViewDataSource protocol. Your data source provides the table-view object with the information it needs to construct and modify a table view. UITableViewDataSource Protocol Reference

Also, looking at your sample code, you hard coded the number of rows in your table to 3. Therefore, UITableView is only going to display 3 rows. You need something like this:

// You have a city array you created
NSMutableArray *cityArray = ......

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

-(IBAction)addCity:(id)sender
{
   // create a new city object
   // add the object to your array
   [cityArray addObject:....
   // refresh the table
   [tableView reloadData];
}
Black Frog
  • 11,595
  • 1
  • 35
  • 66
  • I think he wants to insert individual rows and have a nice animation, your method does not provide for a nice animation. – Ngoan Nguyen Jan 13 '16 at 05:53