How to insert a row into a table?

I am new to objective-c. I have a tableView with 3 rows and 1 section. Can you help me add a row with text to the table by clicking the (addCity) button?

- (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]; } 
+7
source share
2 answers

Your table should take its data from some source, where you can add items when necessary. (let's say an instance of NSMutableArray in your data source), then your methods will look. For simplicity, suppose you have a 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]; } 
+13
source

You do not insert rows directly into the View table. You have accepted the UITableViewDataSource protocol. Your data source provides the table-view object with the information needed to build and change the appearance of the table. Link to UITableViewDataSource Protocol

Also, looking at your sample code, you hardcoded the number of rows in the table to 3. Therefore, the UITableView only displays three 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]; } 
+2
source

All Articles