我在tableViewcontroller类的viewDidLoad
方法中调用web服务,并在另一个类中解析数据,如下所示
- (void)viewDidLoad {
[super viewDidLoad];
dataWebService = [[NSMutableData data] retain];
NSString *authString = [[[NSString stringWithFormat:@"%@:%@",@"admin", @"admin"] dataUsingEncoding:NSUTF8StringEncoding] base64Encoding];
NSMutableURLRequest *request = [[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://172.16.3.47:8980/opennms/rest/alarms?limit=5"]] retain];
[request setValue:[NSString stringWithFormat:@"Basic %@",authString] forHTTPHeaderField:@"Authorization"];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[dataWebService setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[dataWebService appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Eror during connection: %@", [error description]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString* responseString = [[NSString alloc] initWithData:dataWebService encoding:NSUTF8StringEncoding];
AlarmWSParse* alarmParser = [[AlarmWSParse alloc]init];
severity = [alarmParser xmlParser:responseString];
NSLog(@"no of elements in array %d",[severity count]);
NSLog(@"Back to Alarm List View controller and severtiy array is %@",severity);
}
然后将表格单元格中的严重性数组值设置为
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [severity count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
NSString* cellValue = [severity objectAtIndex:indexPath.row];
NSLog(@"cellvalue %@",cellValue);
cell.textLabel.text = cellValue;
return cell;
}
该表仍为空,但NSLog
正确显示所有数据。
此外,在调用和解析Web服务之后,我的应用程序在模拟器中崩溃而在调试器中没有任何错误消息。
如何在数组中向tableview显示这些值?
猜猜是什么..在tabelView:cellForRowAtIndexPath
中包含的cellValue或任何其他nslog消息的nslog消息未在控制台中打印..
这是我的控制台视图
[Session started at 2011-12-22 11:12:07 +0530.]
2011-12-22 11:12:09.358 WebServiceTab[11815:207] do while loopalarm
2011-12-22 11:12:09.359 WebServiceTab[11815:207] severity is MAJOR
2011-12-22 11:12:09.359 WebServiceTab[11815:207] do while loopalarm
2011-12-22 11:12:09.359 WebServiceTab[11815:207] severity is MAJOR
2011-12-22 11:12:09.360 WebServiceTab[11815:207] do while loopalarm
2011-12-22 11:12:09.360 WebServiceTab[11815:207] severity is MAJOR
2011-12-22 11:12:09.360 WebServiceTab[11815:207] do while loopalarm
2011-12-22 11:12:09.360 WebServiceTab[11815:207] severity is MINOR
2011-12-22 11:12:09.361 WebServiceTab[11815:207] do while loopalarm
2011-12-22 11:12:09.361 WebServiceTab[11815:207] severity is MAJOR
2011-12-22 11:12:09.361 WebServiceTab[11815:207] no of elements in array 5
2011-12-22 11:12:09.362 WebServiceTab[11815:207] Back to Alarm List View controller and severtiy array is (
MAJOR,
MAJOR,
MAJOR,
MINOR,
MAJOR
)
我在这里分享了一些源代码文件:https://gist.github.com/fce50a3c4d20cb9c4677 如果您能找到任何错误,请查看。我的应用程序似乎只是崩溃:(
答案 0 :(得分:1)
完成[tableView reloadData]
中的解析后,请尝试connectionDidFinishLoading
。
答案 1 :(得分:0)
检查[severity objectAtIndex:indexPath.row]是否返回NSString,我认为应该是其他的东西
NSString* cellValue = [severity objectAtIndex:indexPath.row];
以及如the.evangelist
所述的尝试[tableView reloadData]答案 2 :(得分:0)
我认为在您的代码表重新加载问题。请在收到webservice的回复后重新加载表。
答案 3 :(得分:0)
我面对很多时间这个问题,我通过这种方法解决了。
1)使用临时数组来保存解析数据
2)分配你的表数组
3)添加到数组的临时数组数据,用于表数组
4)重新加载你的表
答案 4 :(得分:0)
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[dataWebService appendData:data];
[self.tableview reloadData];
}
尝试这可能会起作用....
编辑:
出于崩溃的原因,我认为您没有retain
NSMutableArray
严重性。在尝试那个....之后的viewDidLoad中。
[[severity alloc] init];
[severity retain];