在這種情況下這將如何工作?我創建了一個NSMutabeArray * dataSource;在我的.h文件中,但得到一堆錯誤:如何用Button添加單元格?
「RootViewController.m:錯誤:語義問題:房產‘數據源’上輸入‘* 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];
}
有沒有更好的方法來做到這一點? – Jason 2011-03-17 18:30:55
NSFetchedResultsController。 – 2011-03-17 20:33:21