objective-c - 无法在选项卡栏 Controller 中重新加载 TableView

标签 objective-c ios uitableview uitabbarcontroller reloaddata

您好,我有一个选项卡选项卡 Controller ,我的第一个选项卡包含一个 View :

  • 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 中连接了 TableView ,以便委托(delegate)和数据源连接链接到文件的所有者。

#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;  
}

我已经在这个网站和其他网站上搜索过答案,但我一定是在做一些不同的事情,而且这些解决方案对我没有帮助

最佳答案

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

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

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

@synthesize myTableView;

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

[self.myTableView reloadData];

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

关于objective-c - 无法在选项卡栏 Controller 中重新加载 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7669542/

相关文章:

ios - 如何防止 `pod install` 在不创建新目标的情况下将 libPods.a 添加到 "Link binary with Libraries"构建阶段?

iphone - 更改 iOS 应用的产品名称后出现代码签名错误

ios - 从 iPhone gmail 应用程序打开所有链接打开我的应用程序

ios - 类似于 Facebook/Google 搜索引擎的索引搜索

ios - 有没有办法从这个晦涩的对象中获取属性并将其转换为 JSON?

ios - 在 iOS 中创建消息屏幕

ios - willDisplayCell 不会改变符合逻辑的背景颜色

ios - UITableViewCell 无法使用 systemLayoutSizeFittingSize 获得正确的宽度

ios - 尝试使用点运算符显示来自另一个类的 AppDelegate 中的 NSMutable 数组对象的信息

ios - 共享的 iAd 横幅横幅 ViewDidLoAd 未被调用