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]; }
Vladimir
source share