2011-10-07 5 views
0

SQLite 데이터베이스에서 추출한 정보로 tableView를 채우려 고합니다 ... 일부 자습서를 읽은 후이를 따르려고했지만 몇 가지 이유 때문에 내 앱이 계속 충돌합니다. 나는 그것에서 데이터를 얻을 수 있어요로iOS : SQLite 데이터베이스에서 tableView 채우기

// 
// InOrder.h 
// AGKUnsten 
// 
// Created by Jonas Christensen on 7/12/11. 
// Copyright 2011 __MyCompanyName__. All rights reserved. 
// 

#import <UIKit/UIKit.h> 

@interface InOrder : UIViewController <UITableViewDelegate, UITableViewDataSource> { 

    NSArray *artworkInfo; 
    int rowsInDatabase; 
} 

하는 .m

@property (nonatomic, retain) NSArray *artworkInfo; 

@end 


// 
// InOrder.m 
// AGKUnsten 
// 
// Created by Jonas Christensen on 7/12/11. 
// Copyright 2011 __MyCompanyName__. All rights reserved. 
// 

#import "InOrder.h" 
#import "ArtworkView.h" 
#import "PaintingInfo.h" 
#import "PaintingDatabase.h" 

@implementation InOrder 

@synthesize artworkInfo; 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 

    return 1; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return rowsInDatabase; 
    //return [artworkInfo count]; //Tried to use this, but app just crashes when it reaches this line 
} 

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

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

    NSLog(@"test");//To see how far I get in the code - this is outputted 
    //Configure the cell 

    //APP CRASHES IF I TRY TO DO SOMETHING WITH MY DATABASE IN HERE 

    //cell.textLabel.text = [artworkInfo objectAtIndex:indexPath.row];//Tried this 

    //PaintingInfo *info = [artworkInfo objectAtIndex:indexPath.row];//Tried this 
    //cell.textLabel.text = info.artist; 

    //[[cell textLabel] setText:[artworkInfo objectAtIndex:[indexPath row]]];//Thread 1: Program received signal: "SIGABRT" 

    NSLog(@"test2");//Never reach here if I uncomment any of the above 

    return cell; 
} 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)dealloc 
{ 
    [super dealloc]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
    self.title = @"Værker"; 

    artworkInfo = [[PaintingDatabase database] findAllArtists]; 

    rowsInDatabase = [artworkInfo count]; 
    NSLog(@"%d", rowsInDatabase); 

} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 

    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

@end 

어떤 도움을 크게 감상 할 수있다 .H ..

... 나는, 내 데이터베이스 작동하는 다른 곳에, 하지만

그것은 주로 EXC_BAD_ACCESS 에러 표시의 수 SIGABRT ... 이제

, 나는 것을 들었다있다 ... 내가 여기에 그것을 사용하려고하면 앱이 바로 충돌 것으로 보인다 SIGABORT = "SIGABRT는 프로그램이 자체적으로 중단을 시도 할 때 보내지는 신호입니다. 일반적으로 이것은 실제로 나쁜 것이 발생했기 때문에 발생합니다. "

"이미 릴리스 된 개체로 메시지를 보내면 EXC_BAD_ACCESS가 발생합니다. 오류가 적발되는 시간으로, 호출 스택은 일반적으로

음, 좋은 ". 특히 여러 개의 스레드를 처리하는 경우 사라 ...하지만 난 ... 어떤 도움 ??

답변

1
그것을 해결하지 수행하는 방법

배열이 제대로 유지되도록하는 속성 접근자를 사용하는 대신 artworkInfo (viewDidLoad)에 직접 할당하고 있습니다. 배열은 테이블 뷰 데이터 소스 메서드가 호출 될 때까지 자동으로 해제됩니다 (

).

이어야합니다.

self.artworkInfo = [[PaintingDatabase database] findAllArtists]; 
+0

고마워요! 트릭을 할 것으로 보인다 :) – user969043

0

NSLog(@"%d", rowsInDatabase);이 예상 결과를 표시하는 경우 artworkInfo가 유지되지 않는다고 의심됩니다.

artworkInfo = [[PaintingDatabase database] findAllArtists]; 
:

@property (nonatomic, retain) NSArray *artworkInfo; 

그런 문제가 아마이 라인 : 당신이 (그것이있는 NSArray 아닌있는 NSMutableArray 또는 뭔가 다른 경우) 귀하의 .h 파일에이 방법을 선언했다고 가정

대신에 변경해 :

self.artworkInfo = [[PaintingDatabase database] findAllArtists]; 

차이는 제 실제로 메소드 호출 setArtwork 통해 실행되는 동안 첫 번째 라인은 직접 변수의 값을 할당하는 것입니다 정보 : 비록 그것을 볼 수는 없지만 @synthesize을 사용하면 자동으로 생성됩니다. 속성 선언에 retain이 있으므로이 메서드는 개체의 retain 메서드도 호출하므로 메서드가 끝난 후에도 메모리에 남아 있습니다. 또는이

artworkInfo = [[[PaintingDatabase database] findAllArtists] retain]; 

처럼 직접 같은 일을 그리고 당신은 항상 당신을 마친 때, 유지 객체 어떤 일에 출시 호출해야하기 때문에, 당신의 dealloc 방법에 [artworkInfo release]을 추가해야 할 수 있습니다.

관련 문제