无法在选项卡栏控制器中重新加载表视图
您好,我有一个选项卡选项卡控制器,我的第一个选项卡包含一个视图,其中包含:
- 3 个文本字段
- 一个提交按钮
- 一个 tableView
一旦我填写了文本字段,我单击“提交”,它将信息添加到我的 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 中连接了表视图,以便委托和数据源连接链接到文件的所有者。
#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;
}
我已经在这个网站和其他网站上搜索了答案,但我必须做一些不同的事情,并且解决方案对我没有帮助
Hi I have a tab tab controller and my first tab includes a view with:
- 3 text fields
- a submit button
- a tableView
Once I fill in the text fields I click submit and it adds the information to my managedObjectContext which is an sqlite database (CoreData).
As soon as I click submit I want the tableView to reload to include the added object. Currently my tableView will display the data in the database but it will only add the new row when I stop and re-run the simulator
This is the code for when the add button is tapped, it is here that I can't get the reload tableView working because it says tableView is an undeclared identifier, what have i missed?
-(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];
}
As you can see below I have added the UITableViewDelegate & UITableViewDataSource in the header file. I have also hooked up the tableview in IB so that the delegate and datasource connections are linked to file's owner.
#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
This is the code to fill the tableView which works correctly
#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;
}
I have searched for answers on this site and on others but I must be doing something different and the solutions aren't helping me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 UIViewController 当前没有指向您的表视图的实例变量。设置一个:
记住在您的 .m 中综合它
然后在您的代码中您可以调用
您可能会因为查看使用 UITableViewController 而不是 UIViewController 的代码示例而感到困惑。 UITableViewController 已经有一个名为 tableView 的实例变量,因此您的子类不需要声明它自己的 tableView 实例变量。但是您使用的是 UIViewController,因此您必须声明一个 tableView 实例变量。
Your UIViewController does not currently have an instance variable pointing to your tableview. Set one up:
Remember to synthesize this in your .m
Then in your code you can call
You might have got confused by looking at code examples that use a UITableViewController instead of a UIViewController. The UITableViewController already has an instance variable called tableView, so your subclass wouldn't need it's own tableView instance variable declared. But you're using a UIViewController, so you must declare a tableView instance variable.
感谢@MattyG 的所有帮助。起初我不确定我是否违反了规范,这就是它不起作用的原因。
由于您的建议和建议,我最终解决了问题;它工作完美!我使用调试器,发现虽然我们已经为表创建了一个属性,但我没有创建 IBOutlet 并将其链接到我的 nib 文件中:
我想这意味着我告诉 myTableView 重新加载,但它没有连接到我的表,因此无法使用数据源方法。
Thanks @MattyG for all your help. At first I wasn't sure if I was going against the norm and thats why it wasn't working.
I ended up solving the problem due to your suggestions & it works perfectly! I used the debugger and found that that although we had created a property for the table I had not created an IBOutlet and linked it in my nib file with:
I guess this meant that I was telling myTableView to reload but it wasn't hooked up to my table and thus couldn't use the datasource methods.