0

嗨,我有一个选项卡选项卡控制器,我的第一个选项卡包含一个视图:

  • 3 个文本字段
  • 提交按钮
  • 一个表视图

一旦我填写了文本字段,我点击提交,它将信息添加到我的 managedObjectContext 中,这是一个 sqlite 数据库(CoreData)。

一旦我单击提交,我希望 tableView 重新加载以包含添加的对象。目前我的 tableView 将显示数据库中的数据,但它只会在我停止并重新运行模拟器时添加新行

这是点击添加按钮时的代码,在这里我无法让重新加载 tableView 工作,因为它说 tableView 是一个未声明的标识符,我错过了什么?

-(IBAction)addButtonTapped:(id)sender {
NSLog (@"Add Button Tapped");
NSLog(@"Adding %@ units of item code %@ at $%@ each",quantityTextField.text,productTextField.text,priceTextField.text);


Products_MarketAppDelegate* delegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext* managedObjectContext = delegate.managedObjectContext;
NSManagedObject* newProduct;
newProduct = [NSEntityDescription insertNewObjectForEntityForName:@"Product" inManagedObjectContext:managedObjectContext];

[newProduct setValue:productTextField.text forKey:@"itemCode"];
[newProduct setValue:quantityTextField.text forKey:@"quantity"];
[newProduct setValue:priceTextField.text forKey:@"price"];

if ([managedObjectContext hasChanges]) 
    NSLog(@"Managed Object Changed");

NSError* error;
[managedObjectContext save:&error];

// Insert Reload Table Code Here
// ** I have tried the following and it gives an error "Use of undeclared identifier 'tableView'"
//[tableView reloadData];
//[self.tableView reloadData];

}

正如您在下面看到的,我在头文件中添加了 UITableViewDelegate 和 UITableViewDataSource。我还在 IB 中连接了 tableview,以便委托和数据源连接链接到文件的所有者。

#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>


@interface FirstViewController : UIViewController
<UIApplicationDelegate, UITableViewDataSource,UITableViewDelegate,NSFetchedResultsControllerDelegate>

{
IBOutlet UITextField *productTextField;
IBOutlet UITextField *quantityTextField;
IBOutlet UITextField *priceTextField;

NSMutableArray *items;
NSFetchedResultsController *fetchedResultsController;
}

@property (nonatomic, retain) NSMutableArray *items;
@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;

-(IBAction)addButtonTapped:(id)sender;

@end

这是填充正常工作的 tableView 的代码

#pragma mark TableView
-(NSInteger)numberOfSectionsInTableView: (UITableView *)tableView {
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){   
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];    
}

// Configure the cell
Product* productItem =[fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"%@ x %@ @ $%@",productItem.quantity,productItem.itemCode,productItem.price];

return cell;  
}

我已经在此站点和其他站点上搜索了答案,但我必须做一些不同的事情,并且解决方案对我没有帮助

4

2 回答 2

4

您的 UIViewController 当前没有指向您的 tableview 的实例变量。设置一个:

@property (nonatomic, retain) IBOutlet UITableView *myTableView;

记得在你的 .m 中合成这个

@synthesize myTableView;

然后在你的代码中你可以调用

[self.myTableView reloadData];

查看使用 UITableViewController 而不是 UIViewController 的代码示例,您可能会感到困惑。UITableViewController 已经有一个名为 tableView 的实例变量,因此您的子类不需要声明它自己的 tableView 实例变量。但是您使用的是 UIViewController,因此您必须声明一个 tableView 实例变量。

于 2011-10-06T05:04:00.013 回答
1

感谢@MattyG 的所有帮助。起初我不确定我是否违反了规范,这就是为什么它不起作用。

由于您的建议,我最终解决了问题,并且效果很好!我使用了调试器,发现虽然我们为表创建了一个属性,但我没有创建一个 IBOutlet 并将其链接到我的 nib 文件中:

IBOutlet UITableView *myTableView;

我想这意味着我告诉 myTableView 重新加载,但它没有连接到我的表,因此无法使用数据源方法。

于 2011-10-07T07:03:24.273 回答