我正在創建一個數組。該數組是來自網站的數據。我必須從中剝離一些東西並進行處理(基本上我將公司信息剝離爲每種狀態之一)。使用數組填充UITableView
allStatesFinalArray
是我想在表格視圖中顯示的數組。我可以在fetchedData語句中很好地記錄它。但不能把它放到tableview中。我在表格視圖日誌的日誌中獲得了幾個空響應。爲什麼?我知道我錯過了一些東西。請幫忙。
//
// StateTableViewController.m
//
#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) //1
#define kLatestKivaLoansURL [NSURL URLWithString:@"http://www.sensored.com"] //2
#import "StateTableViewController.h"
#import "ResViewController.h"
@interface StateTableViewController()
@end
@implementation StateTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
dispatch_async(kBgQueue, ^{
NSData *data = [NSData dataWithContentsOfURL:kLatestKivaLoansURL];
[self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});
}
////new code???
NSArray *allStatesFinalArray;
- (NSArray *)fetchedData:(NSData *)responseData
{
//parse out the json data
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSArray *getCompaniesArray = [json objectForKey:@"CompaniesCD"]; //2 get all company info
NSArray *getStatesArray = [getCompaniesArray valueForKey:@"state"];//get only states
NSSet *getOneStateSet = [NSSet setWithArray:getStatesArray];//get rid of duplicates
NSArray* allStatesFinalArray= [getOneStateSet allObjects];//nsset to array
NSLog(@"allstatesfinalarray log 1 : %@", allStatesFinalArray);//return allStatesFinalArray;
return allStatesFinalArray;// return an array of just one state
}
////end newcode???
#pragma mark - Table view data source
- (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 0;
NSLog(@"allstatesfinalarray log 2 : %@", allStatesFinalArray);//return allStatesFinalArray;
return [allStatesFinalArray count];
}
////NOTE ABOVE LOG RETURNS CORRECTLY!!!!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//get log after this step of the array
NSLog(@"here is the list going to tableview: %@", allStatesFinalArray);
static NSString *CellIdentifier = @"stateCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [allStatesFinalArray objectAtIndex:indexPath.row];
return cell;
}
//////NOTE ABOVE LOG RETURNS SEVERAL NILLS????????
- (IBAction)done:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
爲什麼你在'fetchedData:'內部初始化另一個'allStatesFinalArray'? – n00bProgrammer
哦,我試圖從獲取的數據得到任何結果,但不返回任何內容。只有allstatesfinalarray會。 –