2012-03-13 72 views
0

我对此很新,所以请原谅我的无知。IOS将数据从Web服务加载到表中

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

声明我的消息属性的报头

@property (nonatomic, strong) NSArray *messages; 

合成,在M档属性

@synthesize messages; 

这是加载的数据,并假定重新加载表

该方法这个是我告诉它如何加载单元格的方法
- (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"}]} 
+0

是它做什么?或者只是不显示表中的数据? – 2012-03-13 21:35:10

+0

它一路通过,但表从不更新,我添加了一个警报来验证。 – Jhorra 2012-03-13 21:41:54

+0

你可以NSLog message.callback_name或者你知道它被正确的分配吗? – 2012-03-13 22:33:53

回答

0

你有numberOfRowsInSection一个片段?

编辑

一定要初始化数据源(self.messages)

+0

用该方法更新了我的问题。 – Jhorra 2012-03-13 21:42:05

+0

reloadData不会再次调用它吗? – Jhorra 2012-03-13 21:42:30

+0

当您重新载入数据时,它会再次调用它 – 2012-03-13 22:35:04

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]; 
}