我对Objective C很新,所以调试这个问题变得相当令人沮丧。我正在尝试设置一个UITableView,它只是查询数据库并列出返回的对象。我尝试按照Apple的“SQL Books”示例进行示例,但它似乎没有起作用。我很确定此时表格视图根本没有加载数据,因为numberOfRowsInSection正在触发。
以下是.m文件的代码:
#import "MainViewController.h"
#import "CardCell.h"
#import "YgoRulesAppDelegate.h"
#import "Card.h"
@implementation MainViewController
@synthesize managedObjectContext;
@synthesize tblSimpleTable;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
[tblSimpleTable setDelegate:self];
[tblSimpleTable setDataSource:self];
}
// Implement viewWillAppear: to do additional setup before the view is presented. You might, for example, fetch objects from the managed object context if necessary.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller {
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction)showInfo:(id)sender {
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)dealloc {
[managedObjectContext release];
[super dealloc];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
YgoRulesAppDelegate *appDelegate = (YgoRulesAppDelegate *) [[UIApplication sharedApplication] delegate];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Info" message:appDelegate.cards.count delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
return appDelegate.cards.count;
}
// Customize the appearance of the table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
CardCell *cell = (CardCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[[CardCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
YgoRulesAppDelegate *appDelegate = (YgoRulesAppDelegate *)[[UIApplication sharedApplication] delegate];
Card *card = [appDelegate.cards objectAtIndex:indexPath.row];
[cell setCard:card];
// Set up the cell
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
@end
这是.h文件的代码
#import "FlipsideViewController.h"
#import <CoreData/CoreData.h>
@interface MainViewController : UIViewController <FlipsideViewControllerDelegate, UITableViewDelegate, UITableViewDataSource> {
NSManagedObjectContext *managedObjectContext;
IBOutlet UITableView *tblSimpleTable;
}
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) IBOutlet UITableView *tblSimpleTable;
- (IBAction)showInfo:(id)sender;
@end
以下是界面生成器的设置(抱歉,我不会将其作为图片包含在内):http://i.stack.imgur.com/5L7OX.png