2014-02-09 4 views
1

콘솔에서 구문 분석하고 볼 수있는 간단한 JSON 피드가 있지만 테이블 뷰에 표시 할 수없는 것 같습니다. Xcode 5.0.2를 사용하고 있습니다. 나는 콘솔에서 echo되었을 때 왜 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 출력을 볼 수 있다고 말합니다 - 당신도 이것을 볼 수 있습니까? 뉴스 카운트 : N? – plu

+0

아니 뉴스 수는 0입니다, 나는 connectionDidFinishLoading 영역 내부의 카운트를 볼 수 있지만 다른 곳에서는 카운트를 얻지 못합니다. –

+0

mainTableView는 델리게이트와 데이터 소스를 누구에게 말 했나요? – plu

답변

0

귀하의 코드가 inspectio에 의해 확인 보인다 엔. UITableViewController 하위 클래스를 사용하고있는 것처럼 보이지는 않습니다. 따라서 인터페이스 뷰어 나 코드에서 dataSource 및 delegate 속성을 설정해야하므로 tableview라는 테이블 뷰가 있어야합니다. 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을 뿌려 경향이 이러한 종류의 문제가있는 경우.