Iphone 如何添加带有按钮的单元格?

Iphone 如何添加带有按钮的单元格?,iphone,objective-c,uitableview,nsmutablearray,uibarbuttonitem,Iphone,Objective C,Uitableview,Nsmutablearray,Uibarbuttonitem,在这种情况下,这将如何工作?我创建了一个NSMutabeArray*数据源;在my.h文件中,但出现了一系列错误: “RootViewController.m:错误:语义问题:在类型为‘RootViewController*’的对象上找不到属性‘dataSource’” RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController : UITableViewController { NSMuta

在这种情况下,这将如何工作?我创建了一个NSMutabeArray*数据源;在my.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

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

@property (nonatomic,retain) NSMutableArray *dataSource;
并在.m文件中进行合成

@synthesize dataSource;

毫无疑问,一些记忆大师会告诉你为什么@synthesis和mutable数组和字典(大概还有set)不能很好地结合在一起。我只知道,显式初始化可变数组,一切都会好起来:

- (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中释放它。

仍然会出现错误。我使用的是一个tableview模板和一个group table view表格。基本上我只想点击右边导航栏上的“+”按钮并添加一个单元格。我不得不添加一个带有代码的按钮,因为除了表的IB选项外,您无法更改RootViewController.xib。也许这会让事情变得更清楚,谢谢你的快速回答,但它不起作用。也许我提供的信息会更有用。你最初的问题与你在这里的评论非常不同。。。您可能需要编辑您的问题以进行澄清。您是否仍然遇到与问题中列出的相同的错误?我的问题是如何添加带有按钮的单元格。这就是我试图做的…但是由于我无法通过IB向RootViewController.xib添加导航按钮,所以我必须用代码添加它。我只是想弄清楚如何从代码中添加按钮,将单元格添加到组表列表中。我更新了上面的代码,这是我得到的错误:RootViewController.m:28:警告:“NSMutableArray”可能不响应“-insertRowsAtIndexPaths:withRowAnimation:”我还删除了self.navaigationitem.RightBarButtonim.action=这消除了一些错误,但我无法在IB中连接“Addbutton”操作,在代码中我该怎么做?非常感谢。有更好的方法吗?
- (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];
}