如何使用按钮添加单元格?
在这种情况下这将如何运作?我创建了一个 NSMutabeArray *dataSource;在我的 .h 文件中,但出现一堆错误:
“RootViewController.m:错误:语义问题:在“RootViewController *”类型的对象上找不到属性“dataSource””
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];
}
How would this work in this case? I created a NSMutabeArray *dataSource; in my .h file, but getting a bunch of errors:
"RootViewController.m: error: Semantic Issue: Property 'dataSource' not found on object of type '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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一些内存专家无疑能够告诉您为什么 @synthesize 和可变数组和字典(大概还有集合)不能很好地协同工作。我所知道的是,显式初始化你的可变数组,一切都会好起来的:
当然,在 dealloc 中释放它。
Some memory-guru will no doubt be able to tell you why @synthesize and mutable arrays and dictionaries (and sets, presumably) do not play well together. All I know is, initialize your mutable array explicitly and all will be well:
And, of course, release it in the dealloc.
您尚未在 .h 文件中创建属性,并且已将数据源变量与 self 一起使用。
请将 self.dataSource 替换为 dataSource 或为其创建属性。
并合成到.m文件中
You have not created property in .h file and you have used datasource variable with self.
please replace self.dataSource with dataSource or create property for it.
and synthesize in .m file