2011-10-06 51 views
0

您好我有一個標籤選項卡控制器和我的第一個標籤包括一個觀點:不能重裝表視圖在標籤欄控制器

  • 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,這樣代理和數據源連接就會鏈接到文件的所有者。

#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 

這是代碼,以填補其正常工作

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

...我已經尋找在這個網站的答案和別人,但我必須做一些不同,解決方案並不幫助的tableView我

回答

4

你的UIViewController目前沒有指向你的tableview的實例變量。設置一個:

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

記得看代碼示例合成這種在您的m

@synthesize myTableView; 

然後在你的代碼,你可以調用

[self.myTableView reloadData]; 

你可能弄糊塗了使用UITableViewController而不是UIViewController。 UITableViewController已經有一個名爲tableView的實例變量,所以你的子類不需要聲明自己的tableView實例變量。但是你使用的是UIViewController,所以你必須聲明一個tableView實例變量。

+0

好的,謝謝你的幫助,我已經做出了你所建議的改變。這實際上是我之前嘗試過,然後刪除,因爲它仍然沒有把戲。還有什麼我可以嘗試嗎?它沒有提供任何錯誤,但tableView保持不變。我沒有使用導航控制器。文本字段,按鈕和tableView都在相同的視圖,這是一個問題? – msec

+0

不,在同一個視圖上使用textfields,button和tableView是完全正確的。嘗試放置一些NSLog語句來幫助調試。具體來說,把一個放在你的tableView:cellForRowAtIndexPath:方法中。例如NSLog(@「我在方法tableView:cellForRowAtIndexPath:」); – MattyG

+0

好的,謝謝你的提示。我按照你的建議做了,它確認[self.myTableView reloadData]不運行任何與tableView有關的方法。我一直在看文檔,因爲我正在使用NSFetchedResultsController我應該使用_'-(void)controllerDidChangeContent:(NSFetchedResultsController *)controller {self.myTableView endUpdates]; }'_和其他方法來更新表視圖中的更改? – msec

1

感謝@MattyG爲您提供的所有幫助。起初我不確定我是否違背了規範,那就是爲什麼它不起作用。

我最終解決了這個問題,由於你的建議&它完美的工作!我使用的調試和發現,雖然我們創造了一個屬性表我沒有創建一個IBOutlet,並連接它在我的筆尖文件:

IBOutlet UITableView *myTableView; 

我想這意味着我告訴myTableView重裝但它沒有連接到我的表,因此不能使用數據源方法。

+0

不客氣,很高興我能幫到你。好的工作,追蹤出口遺漏。我忘了你可能使用的是界面生成器,而不是在代碼中初始化你的tableview。我已經將IBOutlet添加到了我爲其他人未來參考的答案中。歡呼聲,馬蒂 – MattyG

+0

+ 1爲成功調試需要插座=) – MattyG

相關問題