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];
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…