我正在为iPhone开发一款应用。我有一个模型类数据库,它具有所有sqlite查询方法,如login,register和importAllInboxArticles。对于登录,我有LoginViewController,用于寄存器 - RegisterViewController。我可以在这些类中成功定义Database * db对象,并在ViewDidLoad中成功调用login或register方法。 所以,这很好。
但是,我正在使用ArticleViewController实现相同的逻辑:UITableViewController,它应该调用方法importAllInboxArticles
,形成NSMutable数组,然后显示所有内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {...}
所以我在ViewDidLoad
中调用importAllInboxArticles,但是importAllInboxArticles中的NSLog()语句都没有打印出来。我也尝试在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 :(得分:0)
我想指出您可能需要考虑使用常规UIViewController并将数据源和委托函数放在(a)单独的文件中。否则你的ViewController将变得非常混乱。最佳做法是在所有情况下都避免使用UITableViewController。
如果未调用viewDidLoad,则未正确加载ViewController。您的问题不在您的代码中。如果是,它还应该调用您的数据库方法。您的UITableView如何知道要显示多少个单元格?这是UITableViewController的.m文件的完整列表吗?