2012-03-16 2 views
0

SQLite로 할 일 목록을 만들고 싶습니다.이 자습서를 따라 왔습니다 : http://klanguedoc.hubpages.com/hub/IOS-5-How-To-Display-SQLite-Data-in-a-UITableView 하지만 작동하지 않습니다! 시뮬레이터가 실행되고 앱이 열리지 만 표가 비어 있습니다. 내가 뭔가 잘못하고 있는거야? Snow Leopard 용 xcode 4.2를 사용하고 있습니다.UITableView가 SQLite로 채워지지 않습니다

.sqlite 파일에는 문자열 텍스트, 정수 우선 순위 및 부울 완료 문자가 있습니다. 그러나, 나는 더 단순하게하기 위해 "텍스트"를 구현했습니다. numberOfRowsInSection : 적절한 값을 반환

// Title.h 
#import <Foundation/Foundation.h> 
@interface Title : NSObject { 
NSString *text; 
} 

@property(nonatomic,copy) NSString *text; 

@end 

//TitleVC.h 
#import <UIKit/UIKit.h> 
#import <sqlite3.h> 

@interface TitleVC : UITableViewController{ 

NSMutableArray *thelist; 
sqlite3 *db; 

} 

@property (nonatomic,retain) NSMutableArray *thelist; 

-(NSMutableArray *) toDo; 

@end 

//TitleVC.m 
#import "TitleVC.h" 
#import "Title.h" 
#import <sqlite3.h> 

@implementation TitleVC 
@synthesize thelist; 

- (void)viewDidLoad 
{ 
    [self toDo]; 
    [super viewDidLoad]; 
} 



- (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.thelist count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"TitleCell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 

int rowCount = indexPath.row; 

Title *title = [self.thelist objectAtIndex:rowCount]; 
cell.textLabel.text = title.text; 

return cell; 
} 



-(NSMutableArray *) toDo{ 
thelist = [[NSMutableArray alloc] initWithCapacity:10]; 
@try{ 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSString *dbPath = [[[NSBundle mainBundle] resourcePath  ]stringByAppendingPathComponent:@"todo.sqlite"]; 
    BOOL success = [fileManager fileExistsAtPath:dbPath]; 
    if(!success) 
    { 
     NSLog(@"Cannot locate database file '%@'.",dbPath); 
    } 
    if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK)) 
    { 
     NSLog(@"An error has occured: %@",sqlite3_errmsg(db)); 
    } 

    const char *sql = "SELECT * FROM todo"; 
    sqlite3_stmt *sqlStatement; 
    if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK) 
    { 
     NSLog(@"Problem with prepare statement: %@", sqlite3_errmsg(db)); 
    }else{ 
     Title *title = [[Title alloc] init]; 
     while (sqlite3_step(sqlStatement)==SQLITE_ROW){ 
      title.text = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,1)]; 
      [thelist addObject:title]; 
     } 
    } 
} 
@catch (NSException *exception) { 
    NSLog(@"Problem with prepare statement: %@", sqlite3_errmsg(db)); 
} 
@finally { 
    return thelist; 
} 
} 

답변

0

당신이 표준 jQuery과 사용하는 경우는,있는 tableView입니다 :

여기 내 코드입니까? tableView 란 무엇입니까? cellForRowAtIndexPath : returning? 두 위치 모두에 중단 점을 넣고 메서드가 호출되는 것을 확인합니다.

마지막으로, .xib 파일을 사용하는 경우 UITableView에 대한 연결이 있습니까?

+0

나는 그것을 시도 할 것이다. – sillyparrot

+0

매우 빠른 응답을 해주셔서 대단히 감사합니다. 그러나, 저는 xcode를 처음 접했습니다. 중단 점이 파란색 화살표가 맞습니까? 그래서 나는 돌아 오는 줄에 파란색 화살표를 넣을거야? 값은 아래쪽에 표시되어야합니까? 나는 방금 시뮬레이터를 실행했기 때문에 시뮬레이터가 계속 실행 되었기 때문에 비어있어 아무 것도 볼 수 없습니다. – sillyparrot

+0

오, 제발, 잠깐, 중단 점을 사용하는 방법을 연구했습니다 – sillyparrot

관련 문제