2012-08-15 31 views
0

當按下按鈕時,我在下一個segue中有一個UITableView。我在實現文件中實現的代碼如下。視圖正在移動,但數據未在視圖中加載。數據未在UITableView中加載。可能是什麼問題?

可能是什麼問題?

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     NSData* data = [NSData dataWithContentsOfURL: 
         [NSURL URLWithString: @"https://api.twitter.com/1/statuses/public_timeline.json"]]; 
     NSError* error; 
     tweets = [NSJSONSerialization JSONObjectWithData:data 
               options:kNilOptions 
                error:&error]; 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      [self.tableView reloadData]; 
     }); 
    }); 

    // 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; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 0; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return tweets.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"TweetCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    NSDictionary *tweet = [tweets objectAtIndex:indexPath.row]; 
    NSString *text = [tweet objectForKey:@"text"]; 
    NSString *name = [[tweet objectForKey:@"user"] objectForKey:@"name"]; 
    cell.textLabel.text = text; 
    cell.detailTextLabel.text = [NSString stringWithFormat:@"by %@", name]; 
    return cell; 
} 
+0

你設置了'table委託'和'dataSource'嗎? – janusbalatbat 2012-08-15 11:21:51

+0

您是否檢查過您收到正確的JSON? NSLog(@「%@」,[[[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]); – JulenissensHjelper 2012-08-15 11:31:22

+0

是的,代表和數據源都設置爲tableview – 2012-08-15 11:33:23

回答

1
(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 0; 
} 

你不需要至少1節嗎?

+0

我可能需要稍後。但現在,這只是爲了測試目的。 – 2012-08-15 11:31:33

+0

@MonisManzoor安東尼是非常正確的 - 零部分是不夠的,你**需要**一個。 – 2012-08-15 11:45:32

+0

謝謝,夥計們。這似乎工作一點。但仍然沒有將數據加載到tableview中。它現在在每個單元格中顯示「by null」。 – 2012-08-15 11:58:00

0

您應該使用斷點調試器的numberOfRowsInSection方法突破和cellForRowAtIndexPath驗證您的數據被分析和正確處理。如果您不熟悉斷點,現在是瞭解它們的好機會 - 它們是非常寶貴的,用於調試和解決問題。

作爲一般說明,您可能不應該使用dataWithContentsOfURL來進行聯網。這不是真正爲這種用法設計的。 NSURLConnection是一個更好的選擇,它會給你更多的靈活性。

0
dyspatch_async 

被命名爲是因爲它是異步的。你似乎認爲第二次調用是在第一次調用之後執行的 - 但是沒有。它們是並行執行的,而且由於URL操作速度較慢,因此當沒有數據時會發生表視圖的更新。

解決方案:將reloadData調用放在與URL操作相同的塊中 - 您不需要對dispatch_async進行兩次單獨調用。

此外,你從

numberOfSectionsInTableView: 

返回0,所以表甚至不看數據。返回1.

+0

請詳細說明。我需要在代碼中更改什麼? – 2012-08-15 11:38:29

+0

@MonisManzoor見編輯。 – 2012-08-15 11:47:40

+0

那你爲什麼撤銷+1?你有沒有看到我的回答有問題? – 2012-08-15 12:40:26

相關問題