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];
}
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];
}
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];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With