我是Objective C新手(背景包括perl / php / VB ......非面向对象)。我正在尝试学习Objective C和xcode,并致力于将数据作为测试用例的项目。它使用故事板。
我有一个数据库类,一个单项项目,一个多项项目和一个视图控制器。我删除了默认场景并添加了一个表视图控制器,并选择了控制器作为类。我可以在NSlog中获取要转储的数据,但是我坚持让它在Table View上显示。这是我所拥有的(省略了单项h和m以及AppDelegate文件):
CarbDatabase.h:
#import <Foundation/Foundation.h>
#import "/usr/include/sqlite3.h"
@interface CarbDatabase : NSObject {
sqlite3 *_database;
}
+ (CarbDatabase*)database;
- (NSArray *)FoodItems;
@end
CarbDatabase.m:
#import "FoodItem.h"
#import "CarbDatabase.h"
@implementation CarbDatabase
static CarbDatabase *_database;
+ (CarbDatabase*)database {
if (_database == nil) {
_database = [[CarbDatabase alloc] init];
}
return _database;
}
- (id)init {
if ((self = [super init])) {
NSString *sqLiteDb = [[NSBundle mainBundle] pathForResource:@"food" ofType:@"sqlite3"];
if (sqlite3_open([sqLiteDb UTF8String], &_database) != SQLITE_OK) {
NSLog(@"Failed to open database!");
}
}
return self;
}
- (NSArray *)FoodItems {
NSMutableArray *retval = [[NSMutableArray alloc] init];
NSString *query = @"SELECT ID, LongDesc, ShortDesc, Carb, Sugar, lipid, GrmWt1, GrmWtDesc1, GrmWt2, GrmWtDesc2 FROM food ORDER BY LongDesc";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(_database, [query UTF8String], -1, &statement, nil)
== SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
int uniqueID = sqlite3_column_int(statement, 0);
char *LongDescChars = (char *) sqlite3_column_text(statement, 1);
char *ShortDescChars = (char *) sqlite3_column_text(statement, 2);
int Carb = sqlite3_column_int(statement, 3);
int Sugar = sqlite3_column_int(statement, 4);
int Lipid = sqlite3_column_int(statement, 5);
int GrmWt1 = sqlite3_column_int(statement, 6);
char *GrmWtDesc1Chars = (char *) sqlite3_column_text(statement, 7);
int GrmWt2 = sqlite3_column_int(statement, 8);
char *GrmWtDesc2Chars = (char *) sqlite3_column_text(statement, 9);
NSString *LongDesc = [[NSString alloc] initWithUTF8String:LongDescChars];
NSString *ShortDesc = [[NSString alloc] initWithUTF8String:ShortDescChars];
NSString *GrmWtDesc1 = [[NSString alloc] initWithUTF8String:GrmWtDesc1Chars];
NSString *GrmWtDesc2 = [[NSString alloc] initWithUTF8String:GrmWtDesc2Chars];
FoodItem *info = [[FoodItem alloc] initWithuniqueID:uniqueID LongDesc:LongDesc ShortDesc:ShortDesc Carb:Carb Sugar:Sugar Lipid:Lipid GrmWt1:GrmWt1 GrmWtDesc1:GrmWtDesc1 GrmWt2:GrmWt2 GrmWtDesc2:GrmWtDesc2];
[retval addObject:info];
}
sqlite3_finalize(statement);
}
else
{
NSLog (@"statement failed");
}
return retval;
}
@end
或者Controller.h:
#import <UIKit/UIKit.h>
@interface CarbViewController : UITableViewController
@property (nonatomic, retain) NSArray *FoodItems;
@end
Controller.m或者:
#import "CarbViewController.h"
#import "CarbDatabase.h"
#import "FoodItem.h"
#import "/usr/include/sqlite3.h"
@interface CarbViewController ()
@end
@implementation CarbViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
self.FoodItems = [CarbDatabase database].FoodItems;
self.title = @"Food Items %d",[_FoodItems count];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_FoodItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"FoodItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure the cell...
int rowcount = indexPath.row;
FoodItem *info = [_FoodItems objectAtIndex:indexPath.row];
cell.textLabel.text = info.ShortDesc;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d Carbs per 100 Grams", info.Carb];
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
}
@end
答案 0 :(得分:1)
我不知道是否还有更多问题,但是
numberOfSectionsInTableView:
应返回1,而不是0(否则为表视图
永远都是空的。)
或者,不要实现该方法,因为默认值为1。