2013-08-02 3 views
1

저는 ios를 처음 사용하며 현재 json에서 작업 중입니다. 난 그냥 테이블보기에 표시 할 수있는 아이튠즈 상위 10 앨범을 사용하여 내가 포맷 및 테이블보기에 표시되는 데이터를 받았지만 앨범 이름 만 표시 할 수 있지만 표시 할 특정 앨범의 모든 세부 사항을 표시하고 싶습니다 같은 세포. 여기 xcode의 테이블보기에서 json 데이터를 표시하는 방법

내 전체 코드 내 출력은 같은 것 잘

작업

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    albumtop=[[NSMutableArray alloc]init]; 

    [[self itunestTable]setDataSource:self]; 
    [[self itunestTable]setDelegate:self]; 

    NSURL *url=[NSURL URLWithString:@"https://itunes.apple.com/in/rss/topalbums/limit=10/json"]; 
    NSURLRequest *req=[NSURLRequest requestWithURL:url]; 

    connection=[NSURLConnection connectionWithRequest:req delegate:self]; 
    if(connection) 
    { 
     albumdata =[[NSMutableData alloc]init]; 
    } 
} 

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    [albumdata setLength:0]; 
} 

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

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    NSLog(@"error in connection"); 
} 
-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSDictionary *alldata =[NSJSONSerialization JSONObjectWithData:albumdata options:0 error:nil]; 
    NSDictionary *album =[alldata objectForKey:@"feed"]; 
    NSArray *total =[album objectForKey:@"entry"]; 

    for(NSDictionary *top in total) 
    { 
     NSDictionary *albumname =[top objectForKey:@"im:artist" ]; 
     title =[albumname objectForKey:@"label"]; 
     [albumtop addObject:title]; 
    } 
    [[self itunestTable]reloadData]; 
} 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [albumtop count]; 
} 

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    static NSString *[email protected]"cell"; 
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellidentifier]; 

    if(!cell) 
    { 
     cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier]; 
    } 

    cell.textLabel.text=[albumtop objectAtIndex:indexPath.row]; 
    return cell; 
} 

프로그램이

--------------------------------- 
     Jones 

--------------------------------------- 
    carry Kim 

--------------------------------------- 

내가 할 수있는 내가 모두를 표시 할 수있는 유일한 방법 단일 값을 가져올 수 이 앨범의 자세한 내용

---------------------------------- 
    Jones 
    no.tracks 10 
    price 180 
--------------------------------------- 
    carry Kim 
    no.tracks 10 
    price 180 
--------------------------------------- 

어떻게 도와 드릴까요? 미리 감사드립니다.

+1

연구 문서를 사용해야합니다. –

+0

(힌트 : 먼저 모든 데이터를 하나의 NSMutableArray에 결합합니다 (예 : 각 행의 NSDictionaries). 그런 다음 배열을 테이블 뷰 대리자 클래스에 전달하면됩니다. –

+0

JSON 구문 분석은 내가 위에서 언급 한 배열에 매우 가깝다. 정확하게는 그렇지 않다. 배열은 모든 것에 중심이다.) (즉, 'total'을 사용하라. 'albumTop'을 사용하지 말라. 당신은 필요하고 엔트리 수 등은 맞습니다. cellForRowAtIndexPath 내부에서 특정 행에 필요한 모든 정보를 추출하기 위해'total'을 참조하십시오.) –

답변

0

UITableViewCell에 Thee 레이블을 추가해야합니다.

그리고 필요에 따라 각 값을 가져 오십시오. 이름의 값을 가져 와서 관련 UILabel에 넣으면 간단하게 가져올 수 있습니다. 또한 다음과 같은 방법으로 UITableView의 셀 크기를 확장해야합니다.

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath 
{ 

    return 80;// write as you need. 
} 
+0

실제로 앨범 이름을 배열에 저장하고 인덱스에서 객체에 전달하는 방법 배열에 모든 세부 정보를 저장할 수있는 방법은 무엇입니까? –

+0

@ user2515863- JSON 데이터가 어떻게 보이는지 .. – iPatel

+0

사용 하시겠습니까? url은 json 데이터가 어떻게 보이는지 보려고합니다 –

0

당신은 costum의 UITableviewCells

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BDCustomCell"]; 
if (cell == nil) { 
    // Load the top-level objects from the custom cell XIB. 
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"BDCustomCell" owner:self options:nil]; 
    // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain). 
    cell = [topLevelObjects objectAtIndex:0]; 
} 

return cell; 

}

+0

사용자 정의 셀을 사용해야하지만 어떻게 하나의 전체 앨범 데이터를 테이블 하나의 행에로드 할 수 있는지 알고 있습니다. –

관련 문제