2012-01-27 18 views
0

JSONオブジェクトをダウンロードして解析して、「ニュースフィード」を作成してUITableViewを作成しています。私はconnectionDidFinishLoadingのデリゲートメソッドを持っているコードの非常に最後の行は次のとおりです。connectionDidFinishLoadingが完了した後にUITableViewをリロードできません

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

しかし、中に私のブレークポイント - (UITableViewCellの*)のtableView:(のUITableView *)mytableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法がされていますヒットしません。 (アプリケーションが最初に起動したときにヒットします)

私はメインスレッドでreloadDataを呼び出していますが、何らかの理由でこれを実行します。それは発砲しているように見えません。私はちょうど[tableView reloadData]を試しても動作しませんでした。ここで

は私のconnectionDidFinishLoading方法であって、ここで

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

は私のヘッダファイルである:ここで

#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)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

はいレイ、確かにそうです:それは印刷されました:Tableview is(null) – Flea

+0

私はこれがなぜそうであるか分かりません。私はクラスの最上部で@synthesizeです。 – Flea

答えて

1

を必要とし、全体的な問題は、このようにするときのUITableView、私のnewsItemManager.newsItemCollectionが正しく初期化されていなかったとヌル全体の時間を返してありましたデータをロードしようとしていました。ロードするものはありませんでした。

私はこれをチェックしたと思っていましたが、一日中コンピュータを見つめ、明白なことを見つけられないという問題の1つでした。

2

あなたconnectionDidFinish ...方法はあなたのtableViewControllerクラス内であれば、多分あなただけ

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

私はちょうど自己を追加しようとしました。それと同じ結果に。 – Flea

+0

.hファイルは、tableviewが宣言されている場所、およびinitメソッド(または、あなたがテーブルビューを初期化するときはいつでも)を投稿してください。テーブルにデータが表示されていますか? – Rayfleck

+0

ちょっとレイ、私は私の主な質問を更新し、私のヘッダー情報と実装情報をそこに置く。私はconnectionDidFinishメソッドを使用して、単純な配列の内容を表示するテーブルを得ることができました。 – Flea

関連する問題