Как добавить ячейку с помощью кнопки? - PullRequest
0 голосов
/ 17 марта 2011

Как это будет работать в этом случае?Я создал NSMutabeArray * dataSource;в моем файле .h, но получаю кучу ошибок:

"RootViewController.m: ошибка: семантическая проблема: свойство 'dataSource' не найдено для объекта типа 'RootViewController *'"

RootViewController.h

#import <UIKit/UIKit.h>

@interface RootViewController : UITableViewController {
NSMutableArray *dataSource;
}

@property (nonatomic,retain) NSMutableArray *dataSource;
- (IBAction)addButton:(id)sender;

@end

RootViewController.m

#import "RootViewController.h"

@implementation RootViewController
@synthesize dataSource;

- (void)viewDidLoad
{
[super viewDidLoad];
self.dataSource = [NSMutableArray arrayWithCapacity:1];
//adds right bar button.
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(add:)];
self.navigationItem.rightBarButtonItem=addButton;
[addButton release];

}

-(void)addButton:(id)sender{
[self.dataSource addObject:@"New Item"];
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:[self.dataSource count] inSection:0];
[self.dataSource insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
}

Ответы [ 2 ]

1 голос
/ 17 марта 2011

Некоторые гуру памяти, без сомнения, смогут сказать вам, почему @synthesize и изменяемые массивы и словари (и наборы, предположительно) не очень хорошо играют вместе.Все, что я знаю, это инициализировать ваш изменяемый массив явно, и все будет хорошо:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.dataSource = [NSMutableArray arrayWithCapacity:1];

    //adds right bar button.
    UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(add:)];
    self.navigationItem.rightBarButtonItem=addButton;
    [addButton release];
}

И, конечно, освободить его в dealloc.

0 голосов
/ 17 марта 2011

Вы не создали свойство в файле .h и использовали переменную источника данных с self. замените self.dataSource на dataSource или создайте для него свойство.

@property (nonatomic,retain) NSMutableArray *dataSource;

и синтезировать в .m файле

@synthesize dataSource;
...