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