2015-09-05 108 views
0

我正在創建一個數組。該數組是來自網站的數據。我必須從中剝離一些東西並進行處理(基本上我將公司信息剝離爲每種狀態之一)。使用數組填充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 
+0

爲什麼你在'fetchedData:'內部初始化另一個'allStatesFinalArray'? – n00bProgrammer

+0

哦,我試圖從獲取的數據得到任何結果,但不返回任何內容。只有allstatesfinalarray會。 –

回答

2

你的代碼是一團混亂。你有兩個方法之間聲明的變量:

////new code??? 
NSArray* allStatesFinalArray; 

這使得它成爲一個應用程序的全局變量,這是不好的。

您有2個變量,稱爲allStatesFinalArray;應該是一個實例變量的應用程序全局變量,以及fetchData中根本不應該存在的局部變量。

此舉變量聲明爲您的視圖控制器類的頭:

@interface StateTableViewController() 
{ 
    NSArray* allStatesFinalArray; 
} 
@end 

這使得它的一個實例變量。

現在改變fetchData方法不返回結果:

- (void)fetchedData:(NSData *)responseData 

而且,擺脫局部變量allStatesFinalArray裝修內部fetchData。請fetchData方法將結果保存到allStatesFinalArray實例變量中。

現在,作爲您的fetchData方法的最後一行,請調用您的表視圖的reloadData方法。

+0

謝謝各不相同。是的,確實有效。我知道我剛剛修好了一團糟。進行刷新或重新加載數據有點慢。並且有線線路不會顯示在桌面上。我相信我可以解決這兩個小問題。謝謝 –