2014-02-09 14 views
1

我有一個簡單的JSON提要,我可以在控制檯中解析並看到,但我似乎無法讓它顯示在我的表格視圖中。我正在使用Xcode 5.0.2。我不確定爲什麼我可以在for循環中看到它,而不是在表格視圖中回顯。json元素沒有被傳遞到表視圖

僅供參考,我是全新的xcode和目標c的建築物,我正在使用這個video tutorial作爲指導。

的json是這樣的:

 
[ 
    { 
     "first_name": "Bill" 
    }, 
    { 
     "first_name": "Ted" 
    }, 
    { 
     "first_name": "George" 
    } etc... 
] 

PhotoTableViewController.h

 
#import 
#import "DisplayViewController.h" 

@interface PhotosTableViewController : UITableViewController { 

    IBOutlet UITableView *mainTableView; 

    NSArray *news; 
    NSMutableData *data; 

} 

@end 

PhotoTableViewController.m

 

@interface PhotosTableViewController() { 
    NSMutableArray *photos; 
    NSArray *_locations; 

} 

    - (void)viewDidLoad 
{ 

    NSURL *url = [NSURL URLWithString:@"http://feedurl.json"]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

} 

// some code left out for brevity 


- (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 photos.count; 
    NSLog(@"news count: %lu", news.count); 

    return news.count; 
} 


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

    cell.textLabel.text = [[photos objectAtIndex:indexPath.row] objectForKey:@"first_name"]; 
    NSLog(@"anything: %@", [[photos objectAtIndex:indexPath.row] objectForKey:@"first_name"]); 


    return cell; 

} 



- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    data = [[NSMutableData alloc] init]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData 
{ 
    [data appendData:theData]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 

    news = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 

    NSDictionary *first_name; 

    for (int i=0; i (cant put less than sign here breaks code view) [news count] i++) 
    { 
     first_name = [news objectAtIndex:i]; 
     NSLog(@"Statuses: %@", [first_name objectForKey:@"first_name"]); 
    } 


    [mainTableView reloadData]; 

} 

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"The download could not complete - please make sure you're connected to the internet." delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil]; 

    [errorView show]; 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
} 

+0

乍一看,你的代碼看起來很好。你說你可以在控制檯中看到NSLog輸出 - 你還可以看到這個:news count:N? – plu

+0

沒有新聞計數是0,我可以看到connectionDidFinishLoading區域內的計數,但沒有其他地方,它確實在那裏得到計數。 –

+0

您是否告訴mainTableView誰是委託和數據源? – plu

回答

0

你的代碼看起來的檢測中確定ñ。 您似乎沒有使用UITableViewController子類,因爲您的tableview已命名,所以您必須在Interface Builder或代碼中設置dataSource和委託屬性。在IB中,右鍵單擊tableView對象,並在連接列表頂部有委託和數據源插座。從圓圈拖到每個viewController。在代碼中,你會做,在.m文件:

// Claim that your view controller conforms to the tableview protocols 
@interface YourViewControllerClass() <UITableViewDataSource, UITableViewDelgate> 
... 
// somewhere early on, like in viewDidLoad or viewDidAppear or other initialization code 
mainTableView.delegate = self; 
mainTableView.dataSource = self; 

當我有這幾種我傾向於撒在的tableView方法NSLogs地看到,他們被稱爲(或沒有)的問題。