2015-10-16 50 views
-1

我正在嘗試在tableview中顯示JSON提要。儘管關於json的SO有許多教程和問題,但似乎沒有解決這個問題(沒有提出複雜的框架)。iOS /目標C:在表格中顯示JSON提要

到目前爲止,我已經能夠將JSON提要放入數組中。我知道如何在將對象作爲數據源時顯示錶格。但是,我錯過了將代碼轉換爲適合在表格中顯示的對象的代碼。到目前爲止

代碼:

- (void)viewDidLoad 
    { 
     [super viewDidLoad]; 
//get JSON feed   
     dispatch_async(kBgQueue, ^{ 
      NSData* data = [NSData dataWithContentsOfURL: kLatestKivaLoansURL]; 
      [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES]; 
     }); 
    } 
//convert to array  
    - (void)fetchedData:(NSData *)responseData {  
     NSError* error; 
     NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData 
     NSLog(@"about to print json: %@",json); 
     NSArray* latestLoans = [json objectForKey:@"loans"]; 
     } 
    //Code to convert FEED INTO OBJECT IS NOT WORKING: I have... 
- (NSArray*) convertFeedtoObject:(NSArray*)feed { 
    loanObject *loan = nil; 
    NSMutableArray * loans = [NSMutableArray array]; 
    NSInteger loansCount = 10; 
    int i; 
    for (i = 0; i < loansCount; i++) { 
     [loans addObject: loan] 
} 
} 

    //In the tableview delegate method: 

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     //This sets datasource: 
//getloans method does not exist 
     Loans *loan = [self.getLoans objectAtIndexPath:indexPath]; 
    //This sets place in storyboard VC 
     IDTVCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell"]; 
     cell.loan = loan; 
      if (cell == nil) { 
      cell = [[IDTVCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:@"Cell"]; 
     } 

     NSString * loanTitle = loan.loan; 
     cell.Name = loanTitle; 
     return cell; 
    } 

想知道關於如何得到這個工作的任何建議。

+0

在那裏你調用這個方法convertFeedtoObject,你會得到貸款 –

回答

0

獲取數據後,那麼你需要調用 [self.tableView reloadData]並請寫下來的tableview tableViewNumberOfRows的其他代表和數據源的方法需要對這項工作,並執行設置的tableview數據源和委託自我。

編輯

這應該是你的代碼

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    dispatch_async(kBgQueue, ^{ 
     NSData* data = [NSData dataWithContentsOfURL: kLatestKivaLoansURL]; 
     [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES]; 
    }); 
} 

- (void)fetchedData:(NSData *)responseData {  
    NSError* error; 
    NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData 
    NSLog(@"about to print json: %@",json); 
    NSArray* latestLoans = [json objectForKey:@"loans"]; 
    [self.getLoans removeAllObjects]; 
    self.getLoans = [self convertFeedtoObject:latestLoans]; //add this line 
    } 

- (NSArray*) convertFeedtoObject:(NSArray*)feed { 
loanObject *loan = nil; 
NSMutableArray * loans = [NSMutableArray array]; 
NSInteger loansCount = 10; 
int i; 
for (i = 0; i < loansCount; i++) { 
    [loans addObject: loan] 
} 
[self.tableview reloadData]; //add this line 
} 

//add this method 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  { 

return [self.getLoans count]; 
} 
+0

在我的代碼作爲評論數,我還沒有寫一個getLoans方法,它是隻是一個佔位符。 getLoans會是什麼樣子? – user1904273

相關問題