I'm trying to add a new cell to a UITableview with an IBAction but when I push the add button on the simulator nothing happens. After hours of research on stackoverflow and google I have come up with this code. I think it should work but I can't figure out why it doesn't.
Please help!!!!
My .h file
@interface HomeViewController : UIViewController{
NSMutableArray *countries;
}
- (IBAction)addFav:(id)sender;
@property (nonatomic, strong) IBOutlet UITableView *tableView1;
@end
and my .m file
#import "HomeViewController.h"
@interface HomeViewController ()
@end
@implementation HomeViewController{
}
@synthesize tableView1;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
countries = [NSMutableArray new];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [countries count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"Cell";
UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [countries objectAtIndex:indexPath.row];
return cell;
}
- (IBAction)addFav:(id)sender {
[tableView1 beginUpdates];
[countries addObject:@"Finland"];
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:([countries count] - 1) inSection:0];
[self.tableView1 insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationRight];
[tableView1 endUpdates];
[tableView1 reloadData];
}
@end
Any help would be greatly appreciated
thanks
If you're only adding one item at a time, you can consider modifying your addFav
method to the following:
- (IBAction)addFav:(id)sender {
//[tableView1 beginUpdates];
[countries addObject:@"Finland"];
//NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:([countries count] - 1) inSection:0];
//[self.tableView1 insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationRight];
//[tableView1 endUpdates];
[tableView1 reloadData];
}
Also, you should be registering your HomeViewController
to the UITableViewDelegate
/ UITableViewDataSource
protocols in your .h
file.
Change your line to:
@interface HomeViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>{
Then within IB/Storyboard you need to link the delegate
and dataSource
outlets of your tableView1
under the Connections Inspector to your HomeViewController
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.