2012-01-27 3 views
0

JSON 개체를 다운로드하고 구문 분석하여 "뉴스 피드"를 작성하여 UITableView를 채 웁니다. 나는 connectionDidFinishLoading의 대리자 메서드에있는 코드의 매우 마지막 줄은 다음과 같습니다 그러나connectionDidFinishLoading 완료 후 UITableView를 다시로드 할 수 없습니다.

[tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]; 

, 내 브레이크 포인트에서 - (있는 UITableViewCell *)있는 tableView : (jQuery과 *) mytableView cellForRowAtIndexPath : (NSIndexPath *) indexPath 방법은 맞지 않았다. (응용 프로그램이 처음 시작될 때 히트됩니다.)

주 스레드에서 reloadData를 호출하고 있는데도 이유는 무엇입니까? 발사하는 것처럼 보이지 않습니다. 그냥 [tableView reloadData] 시도하고 작동하지 않았다. 여기

#import <UIKit/UIKit.h> 

@interface NewsViewController : UITableViewController 

@property (nonatomic, retain) NSMutableArray* newsItemArray; 
@property (nonatomic, retain) NSMutableData *responseData; 
@property (nonatomic, retain) IBOutlet UITableView *tableView; 

@end 

내 구현 : 여기에
- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{  

    //NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 

    NSArray *publicTimeline = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil]; 

    NSUInteger newsStreamCount = [publicTimeline count]; 

    // Initialize the collection and the dictionary 
    newsItemManager.newsItemCollection = [[NSMutableArray alloc] initWithCapacity:newsStreamCount]; 
    NSMutableArray *dictOfNewsItems = [[NSMutableArray alloc] initWithCapacity:newsStreamCount]; 

    // From the JSON object, parse out the individual news item JSON objects and 
    // put them into a dictionary. 
    for (int i = 0; i < newsStreamCount; i++) 
    { 
     NSDictionary *item = [publicTimeline objectAtIndex:i]; 
     [dictOfNewsItems addObject:item]; 
    } 

    // For each news item JSON object, extract out the information we need for our 
    // news manager class 
    for (int i = 0; i < newsStreamCount; i++) 
    { 
     NSString *userName = [[dictOfNewsItems objectAtIndex:i] valueForKey:@"Title"]; 
     NSString *message = [[dictOfNewsItems objectAtIndex:i] valueForKey:@"Content"]; 
     NSString *imgUrl = [[dictOfNewsItems objectAtIndex:i] valueForKey:@"https://si0.twimg.com/logo_normal.jpg"]; 


     NewsItem *newsItem = [[NewsItem alloc] initWithBasicInfo:userName :message :imgUrl]; 


     [newsItemManager.newsItemCollection addObject:newsItem]; 
    } 

    [tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]; 
} 

내 헤더 파일 : 여기에

내 connectionDidFinishLoading 방법입니다

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Uncomment the following line to preserve selection between presentations. 
    // self.clearsSelectionOnViewWillAppear = NO; 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 

    self.title = @"News"; 

    //self.newsItemArray = [[NSMutableArray alloc] initWithObjects:@"One", @"Two", @"Three", nil]; 

    tableView.rowHeight = 75.0; 


    responseData = [NSMutableData data];   
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://blah.com/api/news"]]; 
    (void)[[NSURLConnection alloc] initWithRequest:request delegate:self]; 

} 

감사합니다, 벼룩

+1

performSelector를 호출 할 때 어떤 이유로 tableView가 nil입니까? NSLog (@ "Tableview is % @", tableView); – Rayfleck

+0

예 Ray, 확실히 다음과 같이 보입니다. 인쇄 : Tableview is (null) – Flea

+0

이것이 사실 인 이유는 확실하지 않습니다. 나는 클래스 상단에서 @synthesize입니다. – Flea

답변

1

필요, 당신의 tableViewController 클래스 내부에 따라서 때 jQuery과 데이터를로드하려고 시도했습니다. 로드 할 것이 아무것도 없었습니다.

나는 이것을 위해 컴퓨터를 하루 종일 쳐다보고 분명한 것을 놓친 문제 중 하나를 확인했다고 생각했습니다.

2

당신의 connectionDidFinish 경우 ... 방법은 어쩌면 당신은 그냥 전반적인 문제는 내 newsItemManager.newsItemCollection가 제대로 초기화되지 하였다 및 널 전체 시간을 반환했다

[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]; 
+0

방금 ​​자기 소개를 추가하려고했습니다. 그것과 동일한 결과. – Flea

+0

.h 파일은 tableview가 선언 된 곳과 init 메소드 (또는 사용자가 tableview를 초기화 할 때마다)를 게시하십시오. 테이블에 데이터가 표시됩니까? – Rayfleck

+0

안녕하세요 레이, 내 주요 질문을 업데이 트하고 거기에 내 헤더 정보 및 구현 정보를 넣어. connectionDidFinish 메서드를 사용하지 않고 간단한 배열의 내용을 표시하도록 테이블을 가져올 수있었습니다. – Flea

관련 문제