IOS таблицы и массивы JSON - PullRequest
       4

IOS таблицы и массивы JSON

0 голосов
/ 21 марта 2012

Это сбило меня с толку за хитрость

Я успешно заполняю массив из вызова json, но табличное представление, которое я хочу собрать, не выполняется как.

Кто-нибудь может понять почему?

Я пробовал несколько хороших вещей, но массив, передаваемый в tableView, не заполняется успешно полученными значениями json

 - (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    self.responseData = nil;

    NSArray* categories = [(NSDictionary*)[responseString JSONValue] objectForKey:@"categories"];
    [responseString release];


    //fetch the data
    for (NSDictionary* item in categories) {        
        NSString* c = [item objectForKey:@"CATEGORY_NAME"];
        [self.tableViewArray addObject:[NSString stringWithFormat:@"%@", c]];
    }

}

    - (void)viewDidLoad
{
    [super viewDidLoad];
    [self loadData];
}

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [tableViewArray count];
}

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    // Configure the cell.
    NSInteger rowNumber = indexPath.row;
    NSString *stateName = [tableViewArray objectAtIndex:rowNumber];
    cell.textLabel.text = stateName;
    return cell;
 }

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *message = [NSString stringWithFormat:@"You selected %@",[self.tableViewArray objectAtIndex:indexPath.row]];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message: message delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];

    [alert show];

    [alert release];


}

EDIT

Мой .ч

@interface Medical_MeetingsViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
    NSMutableArray *tableViewArray; 
    NSMutableData *responseData; 
    IBOutlet UITableView *tableViewCat;
}

@property (nonatomic, retain) NSMutableArray *tableViewArray;
@property (retain, nonatomic) NSMutableData *responseData;

-(IBAction)loadData;

@end

это правильно?

1 Ответ

1 голос
/ 21 марта 2012

Две вещи:

В конструкторе у вас есть TableView, подключенный через IBOutlet? Эта ссылка необходима для того, чтобы ваш код мог отправить ему инструкции.

Во-вторых, я не вижу, чтобы вы звонили [tableview reloadData], вам нужно будет позвонить после того, как вы закончите сбор данных.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...