我正在嘗試在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;
}
想知道關於如何得到這個工作的任何建議。
在那裏你調用這個方法convertFeedtoObject,你會得到貸款 –