gpt4 book ai didi

iphone - 使用 XML 中的数据填充 UITableView

转载 作者:行者123 更新时间:2023-11-29 04:29:17 25 4
gpt4 key购买 nike

在我的 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];



}];

最佳答案

因为您没有将标签、文本...等添加到单元格中...

[cell addSubview:cellLabelSubject];
[cell addSubview:cellLabelChannel];
[cell addSubview:cellTextViewText];
[cell addSubview:cellLabelTime];
[cell addSubview:cellLabelSender];
[cell addSubview:cellImage];

关于iphone - 使用 XML 中的数据填充 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11929710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com