2014-06-10 95 views
0

我正在爲iPhone開發一個應用程序。我有一個模型類數據庫,它具有所有的sqlite查詢方法,如登錄,註冊和importAllInboxArticles。對於登錄,我有LoginViewController,用於註冊 - RegisterViewController。我可以在這些類中成功定義Database * db對象,並在ViewDidLoad中成功調用登錄或註冊方法。 所以,這很好。在ViewController中調用模型方法iOS

不過,我採取相同的邏輯與ArticleViewController:UITableViewController中,這應該調用方法importAllInboxArticles,形成NSMutable陣列,然後顯示一切都在

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

所以我呼籲importAllInboxArticles在ViewDidLoad,但即使沒有的NSLog ()語句在importAllInboxArticles不會被打印。我也嘗試調用cellForRowAtIndexPath中的importAllInboxArticles,但沒有結果。

Database.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方法,比如在其他意見? 謝謝。

+0

你能告訴我們在哪兒,當'self.db'被定義爲'ÀrticleViewController'?它可能是'null'。 – katzenhut

+0

self.db是數據庫對象,它定義在ÂrticleViewController.h中,並且我懶惰地將它初始化在ÂrticleViewController.m中。謝謝。 – sermilion

+0

您是否在日誌中看到「inboxArticles%d中的文章數量」?是否在日誌中調用了「文章%d中的文章數量」? 因爲如果viewDidLoad沒有被調用,你的ViewController加載不正確。 –

回答

0

我想指出,你可能想要考慮使用一個普通的UIViewController,並將數據源和委託函數放在(a)單獨的文件中。否則你的ViewController將變得非常混亂。最佳做法是在所有情況下避免使用UITableViewController。

如果viewDidLoad沒有被調用,你的ViewController沒有被正確加載。你的問題不在你的代碼中。如果是這樣,它也應該調用你的數據庫方法。你的UITableView如何知道要顯示多少個單元格?這是UITableViewController的.m文件的完整列表嗎?

如果是這樣你缺少的實現的功能所需的最低量:https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UITableViewDataSource/tableView:numberOfRowsInSection

+0

嗨,我已經添加完整的ArticleViewController。而且,我已經就上面發現的一些錯誤提供了評論。謝謝。 – sermilion

相關問題