开发者

Can't reload Table View in tab bar controller

开发者 https://www.devze.com 2023-04-10 19:54 出处:网络
Hi I have a tab tab controller and my first tab includes a view with: 3 text fields a submit button a tableView

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:UITableViewCell开发者_如何学CStyleDefault 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


Your UIViewController does not currently have an instance variable pointing to your tableview. Set one up:

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

Remember to synthesize this in your .m

@synthesize myTableView;

Then in your code you can call

[self.myTableView reloadData];

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.


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:

IBOutlet UITableView *myTableView;

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号