在ViewController iOS中调用模型方法

在ViewController iOS中调用模型方法,ios,objective-c,Ios,Objective C,我正在为iPhone开发一个应用程序。我有一个模型类数据库,它有所有sqlite查询方法,比如login、register和importAllInboxArticles。对于登录,我有LoginViewController,对于register-RegisterViewController。我可以在这些类中成功地定义Database*db对象,并在ViewDidLoad中成功地调用login或register方法。 所以,这很好 然而,我使用ArticleViewController实现了相同的

我正在为iPhone开发一个应用程序。我有一个模型类数据库,它有所有sqlite查询方法,比如login、register和importAllInboxArticles。对于登录,我有LoginViewController,对于register-RegisterViewController。我可以在这些类中成功地定义Database*db对象,并在ViewDidLoad中成功地调用login或register方法。 所以,这很好

然而,我使用ArticleViewController实现了相同的逻辑:UITableViewController,它应该调用方法
importAllInboxArticles
,形成NSMutable数组,然后在中显示所有内容

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {...}
所以我在
ViewDidLoad
中调用importAllInboxArticles,但即使是importAllInboxArticles中的NSLog()语句也不会被打印出来。我还尝试调用cellForRowAtIndexPath中的importAllInboxArticles,但没有结果

数据库.m

- (NSMutableArray*)importAllInboxArticles:(int)user_id
{

    NSLog(@"Start,Article Importing");

    NSMutableArray *inboxArticles = [[NSMutableArray alloc]initWithCapacity:20];
    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM Article AS A JOIN UserArticle AS UA ON UA.article_id=A.id WHERE user_id=?"];
    sqlite3_stmt *select;

    int result = sqlite3_prepare_v2(self.db, [sql UTF8String], -1, &select, NULL);

    if (result == SQLITE_OK) {
        sqlite3_bind_int (select, 3, (NSInteger)user_id);

        NSLog(@"Article Importing SQL: %d, SQLITE_ROW: %d", result, SQLITE_ROW);

        while (sqlite3_step(select) == SQLITE_ROW) {
            NSLog(@"In Article Import While");
            NSMutableArray *values = [[NSMutableArray alloc] initWithCapacity:6];

            // add the id:
            [values addObject:
             [NSString stringWithFormat:@"%s", sqlite3_column_text(select, 0)]];
            // add the title:
            [values addObject:
             [NSString stringWithFormat:@"%s", sqlite3_column_text(select, 1)]];
            // add the content:
            [values addObject:
             [NSString stringWithFormat:@"%s", sqlite3_column_text(select, 2)]];
            // add the author:
            [values addObject:
             [NSString stringWithFormat:@"%s", sqlite3_column_text(select, 3)]];
            // add the date:
            [values addObject:
             [NSString stringWithFormat:@"%s", sqlite3_column_text(select, 4)]];
            // add the tags:
            [values addObject:
             [NSString stringWithFormat:@"%s", sqlite3_column_text(select, 5)]];



            Article* article = [[Article alloc]init];
            article.article_id = [[values objectAtIndex:0]integerValue];
            article.title = [values objectAtIndex:1];
            article.content = [values objectAtIndex:2];
            article.author = [values objectAtIndex:3];
            article.date = [values objectAtIndex:4];
            article.url = [values objectAtIndex:5];
            article.tags = [values objectAtIndex:6];
            [inboxArticles addObject:article];
            NSLog(@"Article added.");

            NSLog(@"Sucsefful loginnnn! %@", [NSString stringWithFormat:@"%s", sqlite3_column_text(select, 1)] );


        }

    }
    NSLog(@"%d articles were imported form db", inboxArticles.count);
    return inboxArticles;
}
ArticleViewController.m

   //
//  ArticleViewController.m
//  ReadLater
//
//  Created by Ibragim Gapuraev on 09/06/2014.
//  Copyright (c) 2014 Sermilion. All rights reserved.
//

#import "ArticleViewController.h"
#import "LoginViewController.h"

@interface ArticleViewController ()

@end

@implementation ArticleViewController

@synthesize db,articles;


//- (id)init{
//    self = [super init];
//    if (!self) {
//        self.db = [Database init];
//        self.articles = [[NSMutableArray alloc]init];
//    }
//    return self;
//}

- (NSMutableArray* ) articles
{
    if (!articles) {
        articles = [[NSMutableArray alloc] initWithCapacity:20];
    }

    return articles;
}

- (Database* ) db
{
    if (!db) {
        db = [[Database alloc] init];
    }
    return db;
}


//- (id)initWithStyle:(UITableViewStyle)style
//{
//    self = [super initWithStyle:style];
//    if (self) {
//        
//    }
//    return self;
//}

- (void)setInboxList:(NSMutableArray* )inboxList
{
    self.articles = inboxList;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.articles = [self.db importAllInboxArticles:16];
    NSLog(@"Number of articles in inboxArticles %d", articles.count);
    // Do any additional setup after loading the view.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.articles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{



    NSLog(@"Number of articles in articles %d", self.articles.count);
    static NSString *CellIdentifier = @"Content";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    NSInteger rowIndex = indexPath.row;

    // Configure the cell...
    Article* article = [self.articles objectAtIndex:rowIndex];
    NSString *listingKey = article.title;
    //NSString *listingKey = [[[MAIN_CONTROLLER partsCatalog].listing allKeys] objectAtIndex:indexPath.row];
    NSString *listingValues = article.url;
    cell.textLabel.text = listingKey;
    cell.detailTextLabel.text = listingValues ;

    return cell;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}





@end
那么,为什么我不能像在其他视图中一样调用ArticleViewController中的方法呢?
谢谢。

< P>我想指出,你可能想考虑使用一个规则的UIVIEW控制器,并将数据源和委托函数放在(A)单独的文件中。否则,ViewController将变得非常混乱。最佳做法是在所有情况下都避免使用UITableViewController

如果未调用viewDidLoad,则未正确加载ViewController。您的问题不在代码中。如果是,它还应该调用您的数据库方法。UITableView如何知道要显示多少单元格?这是UITableViewController的.m文件的完整列表吗


如果是这样,您缺少所需的最低实现功能量:

您能告诉我们为
articleviewcontroller
定义了
self.db
的位置和时间吗?它可能是
null
。self.db是数据库对象,它在ÀrticleViewController.h中定义,我会在ÀrticleViewController.m中惰性地初始化它。谢谢。您的日志中是否有“收件箱中的文章数%d”?日志中是否调用了“文章%d中的文章数”?因为如果没有调用viewDidLoad,您的ViewController将无法正确加载。我想,我发现了问题,没有正确初始化db。现在我写了…db=[[Database alloc]init];。。。数据库被初始化。现在,我得到所有的打印行,直到if(result==SQLITE\u OK){..,我想,这意味着查询没有返回任何结果。但是当我复制查询并在sqlite3终端中运行它时,点击返回结果。我发现了最初的问题-我没有打开和关闭数据库。现在一切都正常了。谢谢大家的评论。对于这样一个愚蠢的错误,我将在8小时后回答mmy的问题,因为系统现在不允许我这样做。您好,我已经添加了完整的ArticleViewController。并且,我已经对上面我发现的一些错误提供了评论。谢谢。