Я создаю массив. Массив - это данные с веб-сайта. Мне нужно снять с него материал и обработать его (в основном, информацию о компании, которую я разделяю только на одно из каждого состояния).Заполнение UITableView массивом
allStatesFinalArray
- это массив, который я хочу отобразить на столе. Я могу записать его в файл fetchedData. Но он не может попасть в таблицу. Я получаю несколько отрицательных ответов в журнале из журнала таблиц. Зачем? Я знаю, что чего-то не хватает. Пожалуйста помоги.
//
// 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
Почему вы инициализировали другой 'allStatesFinalArray' внутри' fetchedData: '? – n00bProgrammer
oh Я попытался получить какой-либо результат из полученных данных, но ничего не возвращает. Только allstatesfinalarray. –