1

我是一个 Objective C 新手(背景包括 perl/php/VB...非面向对象)。我正在尝试学习 Objective C 和 xcode,并从事一个将数据作为测试用例的项目。它使用故事板。

我有一个数据库类,一个用于单个项目的类和一个用于多个项目的类和一个视图控制器。我已经删除了默认场景并添加了一个表格视图控制器并选择了控制器作为它的类。我可以将数据转储到 NSlog 中,但我一直坚持让它显示在表视图上。这是我所拥有的(省略了单个项目 h 和 m 以及 AppDelegate 文件):

碳水化合物数据库.h:

#import <Foundation/Foundation.h>
#import "/usr/include/sqlite3.h"


@interface CarbDatabase : NSObject {
    sqlite3 *_database;
}


+ (CarbDatabase*)database;
- (NSArray *)FoodItems;

@end

碳水化合物数据库.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

控制器.h:

#import <UIKit/UIKit.h>

@interface CarbViewController : UITableViewController

@property (nonatomic, retain) NSArray *FoodItems;

@end

控制器.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
4

1 回答 1

1

不知道是不是还有更多的问题,但是 numberOfSectionsInTableView:应该返回1,而不是0(否则table view会一直是空的)。

或者,不要实现该方法,因为默认值为 1。

于 2013-08-12T13:54:58.363 回答