如何使用按钮添加单元格?

发布于 2024-10-22 20:24:44 字数 1213 浏览 5 评论 0原文

在这种情况下这将如何运作?我创建了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔一刀 2024-10-29 20:24:44

一些内存专家无疑能够告诉您为什么 @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 中释放它。

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:

- (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];
}

And, of course, release it in the dealloc.

做个少女永远怀春 2024-10-29 20:24:44

您尚未在 .h 文件中创建属性,并且已将数据源变量与 self 一起使用。
请将 self.dataSource 替换为 dataSource 或为其创建属性。

@property (nonatomic,retain) NSMutableArray *dataSource;

并合成到.m文件中

@synthesize dataSource;

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.

@property (nonatomic,retain) NSMutableArray *dataSource;

and synthesize in .m file

@synthesize dataSource;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文