【发布时间】:2013-05-30 00:49:20
【问题描述】:
我在tableViewcontroller 类的viewDidLoad 方法中调用一个webservice,并在另一个类中解析数据如下
- (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 如果您能找到任何错误,请查看。我的应用似乎只崩溃了:(
【问题讨论】:
-
如果有人可以编辑我的问题的代码部分,我将不胜感激,因为它没有以易于理解的彩色方式显示
-
请重新格式化。读起来很可怕:)
-
我试过了,但我不知道如何编辑它。
-
@TechnocraT:突出显示代码并按下
{}按钮。 -
方法名称之前的 - 符号变成了项目符号,因此其余代码变成了这种方式..
标签: iphone objective-c web-services uitableview