在我的 Iphone 应用程序中,我想用从 Web 服务获取的 XML 文件中的数据填充 UITableView。我已经得到了正确的数据,我也可以用 RaptureXML 解析它。我在(登录/授权)之前进行了一些数据库查询,然后获取了 Xml(带有消息的文件)。我的问题是 UITableView 没有得到任何数据。
这是我将数据填充到表中的方法:
if([responseString isEqualToString:@"true"])
{
NSLog(@"Iphone successfully registered !");
NSURL *baseURL = [NSURL URLWithString:@"http://pathtomywebservice/"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
[httpClient defaultValueForHeader:@"Accept"];
[httpClient setDefaultHeader:@"Accept" value:@"text/xml"];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
udid, @"uuid",
nil];
[httpClient postPath:@"getvMess" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
// reponseObject will hold the data returned by the server.
NSString *responseString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"response: %@", responseString);
RXMLElement *rxml = [RXMLElement elementFromXMLString:responseString encoding:NSUTF8StringEncoding];
subjects = [[NSMutableArray alloc] init];
senders = [[NSMutableArray alloc] init];
texts = [[NSMutableArray alloc] init];
times = [[NSMutableArray alloc] init];
channels = [[NSMutableArray alloc] init];
NSArray *apps = [rxml children:@"ifxMessage"];
[rxml iterateElements:apps usingBlock:^(RXMLElement *appElement) {
[subjects addObject:[appElement child:@"subject"].text];
NSLog(@"%@",[appElement child:@"subject"].text);
[times addObject:[appElement child:@"time"].text];
[texts addObject:[appElement child:@"text"].text];
[senders addObject:[appElement child:@"sender"].text];
[channels addObject:[appElement child:@"receiver"].text];
}];
}failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error retrieving data: %@", error);
}];
}
}failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error retrieving data: %@", error);
}];
这里是填写表格的具体代码:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return [subjects count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Configure the cell...
// A cell identifier which matches our identifier in IB
static NSString *CellIdentifier = @"CellIdentifier";
// Create or reuse a cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Get the cell label using its tag and set it
UILabel *cellLabelSubject = (UILabel *)[cell viewWithTag:1];
[cellLabelSubject setText:[subjects objectAtIndex:indexPath.row]];
//Get the cell's channel and its tag and set it
UILabel *cellLabelChannel = (UILabel *)[cell viewWithTag:3];
[cellLabelChannel setText:[channels objectAtIndex:indexPath.row]];
//Get the cell's text and its tag and set it
UITextView *cellTextViewText = (UITextView *)[cell viewWithTag:4];
[cellTextViewText setText:[texts objectAtIndex:indexPath.row]];
//Get the cell's time and its tag and set it
UILabel *cellLabelTime = (UILabel *)[cell viewWithTag:5];
[cellLabelTime setText:[times objectAtIndex:indexPath.row]];
//Get the cell's sender and its tag and set it
UILabel *cellLabelSender = (UILabel *)[cell viewWithTag:6];
[cellLabelSender setText:[senders objectAtIndex:indexPath.row]];
// get the cell imageview using its tag and set it
UIImageView *cellImage = (UIImageView *)[cell viewWithTag:2];
[cellImage setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%new_message.png", indexPath.row]]];
return cell;
}
但是当我从本地 xml 文件填充表格时,它工作正常:
subjects = [[NSMutableArray alloc] init];
senders = [[NSMutableArray alloc] init];
texts = [[NSMutableArray alloc] init];
times = [[NSMutableArray alloc] init];
channels = [[NSMutableArray alloc] init];
RXMLElement *rxml = [RXMLElement elementFromXMLFile:@"Messages.xml"];
NSArray *apps = [rxml children:@"ifxMessage"];
[rxml iterateElements:apps usingBlock:^(RXMLElement *appElement) {
[subjects addObject:[appElement child:@"subject"].text];
[times addObject:[appElement child:@"time"].text];
[texts addObject:[appElement child:@"text"].text];
[senders addObject:[appElement child:@"sender"].text];
[channels addObject:[appElement child:@"receiver"].text];
}];