IOS将数据从Web服务加载到表中

时间:2012-03-13 21:33:00

标签: json ios5 uitableview

我对此很陌生,所以原谅我的无知。

我有以下方法拼凑在一起,运行时没有错误,我验证了我从Web服务获取数据。我注意到的是当我在numberOfRowsInSection方法上放置一个断点时,它会两次返回0。我认为这意味着我没有正确加载我的数组。

在标题

中声明我的消息属性
@property (nonatomic, strong) NSArray *messages; 

在m文件中合成该属性

@synthesize messages;

这是加载数据的方法,应该重新加载表

- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response 
{   
    if ([response isOK]) 
    {  
        // Success! Let's take a look at the data
        NSDictionary *result = [NSJSONSerialization JSONObjectWithData: [[response bodyAsString] dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableContainers error:nil];

        if([[result objectForKey:@"result"] isEqualToString:@"success"])
        {
            NSArray *message_array = [result objectForKey:@"messages"];

            for(NSUInteger i = 0; i < [message_array count]; i++)
            {
                EHRXMessage *m = [[EHRXMessage alloc] init];
                NSDictionary *message = [message_array objectAtIndex: i];
                m.subject = [message objectForKey:@"Title"];
                m.callback_name = [message objectForKey:@"CallbackName"];

                [self.messages setValue:m forKey:[NSNumber numberWithInt:i]];
            }


            [self.tableView reloadData];
        }
        else
        {
            UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Inbox Error!"
                                                            message:@"Error loading messages"
                                                           delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        }

    } 
}

这是我告诉它如何加载单元格的方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MessageCell"];
    EHRXMessage *message = [self.messages objectAtIndex:indexPath.row];

    cell.textLabel.text = message.callback_name;
    cell.detailTextLabel.text = message.subject;
    return cell;
}


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

这是json看起来像什么的片段

{"result":"success","count":"0", "messages":[{"SenderID":"6fcb7c19-b21f-4640-a237-0e7ac5ca0ce8","Title":"General","Message":"","CallbackName":"Claudia","CallbackNumber":"1295","EncounterID":null,"AdmissionID":"d7387243-3e8a-42e4-8a85-fdd3428dae68","DateSent":"3/9/2012 12:52 PM"}]}

2 个答案:

答案 0 :(得分:1)

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.loaded = FALSE;

    NSURL *URL = [NSURL URLWithString:@"http://46.136.168.166:8000/test.php"];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [connection start];

    // creating loading indicator
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];

    if(connection){
        self.recievedData = [NSMutableData data];
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if(!cell)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

    NSDictionary *item = [self.dataBase objectAtIndex:[indexPath section]];

    NSArray *item_d = [item allKeys];
    [[cell textLabel] setText:[item objectForKey:[item_d objectAtIndex:[indexPath row]]]];
    return cell;
}
- (void)fetchedData:(NSMutableData *)responseData
{
    NSError *error;    
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
    [self setDataBase:[json objectForKey:@"json-data-header"]];
    [self.table reloadData];
}

答案 1 :(得分:0)

你有numberOfRowsInSection吗?

修改

请务必初始化数据源(self.messages)