Я пытаюсь добавить новую ячейку в UITableview с помощью IBAction, но когда я нажимаю кнопку добавления на симуляторе, ничего не происходит. После нескольких часов исследований stackoverflow и google я придумал этот код. Я думаю, что он должен работать, но я не могу понять, почему это не так.Как добавить ячейку в uitableview
Пожалуйста, помогите !!!!
Мой .h файл
@interface HomeViewController : UIViewController{
NSMutableArray *countries;
}
- (IBAction)addFav:(id)sender;
@property (nonatomic, strong) IBOutlet UITableView *tableView1;
@end
и мой файл .m
#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
Любая помощь будет принята с благодарностью
Thanks
Jst try' countries = [[NSMutableArray alloc] init]; 'in' viewDidLoad'. – HRM
благодарит Ox за быстрый ответ, который остановил приложение от сбоев, но когда я нажал кнопку добавления, ячейка не добавлена , – Shayno
Спасибо HRM, я попробовал это, и приложение не сработает снова, но ячейка не добавится. – Shayno