【问题标题】:Displaying parsed values from webservice in tableview在 tableview 中显示来自 web 服务的解析值
【发布时间】: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


【解决方案1】:

在完成connectionDidFinishLoading 的解析后尝试[tableView reloadData]

【讨论】:

  • 正如我提到的,应用程序在解析数据后不断崩溃。我也试过 [self.tableView reloadData];在 conncectionDidFinishLoading 方法中。但不知道应用程序崩溃时发生了什么..
  • 1.注释“[连接释放];”行在 connectionDidFinishLoading 中。 2.阅读解析器代码,看看是否正确解析返回的数据,即没有内存泄漏等。
【解决方案2】:

检查 [severity objectAtIndex:indexPath.row] 是否返回 NSString ,我认为应该是别的东西

 NSString* cellValue = [severity objectAtIndex:indexPath.row];

以及尝试 the.evangelist 提到的 [tableView reloadData]

【讨论】:

  • 您可以看到已编辑的问题,cellValue 只是没有在控制台中打印出来。任何解决方案??
  • NSLog(@"%@",severity);你能在你的代码中添加这个给我控制台数据,这样我就可以帮助你,只是想知道数据的严重性
  • 正如您在我编辑的问题中看到的,最后一个值是控制台中严重性的输出。
  • [self.tableview reloadData]; in - (void)connectionDidFinishLoading:(NSURLConnection *)connection 函数在最后一行,因为它会在您收到完整数据后重新加载数据,如果您在其他地方有相同的命令,只需将其删除
  • 完成它仍然应用程序崩溃而不在表中显示任何数据。另外我托管的源代码文件很少,请看看它是否有帮助。 gist.github.com/fce50a3c4d20cb9c4677
【解决方案3】:

我多次遇到这个问题,我通过这种方法解决了。

1) 使用临时数组保存解析数据
2) 分配您的表数组
3) 临时数组数据添加到您的数组中,用于表数组
4)重新加载你的表

【讨论】:

    【解决方案4】:
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [dataWebService appendData:data];
    [self.tableview reloadData];
    }
    

    试试这个可能会奏效....

    编辑:

    由于您的崩溃,我认为您没有retain 您的NSMutableArray 严重性。尝试后在 viewDidLoad 中......

    [[severity alloc] init];
    [severity retain];
    

    【讨论】:

    • 不起作用,伙计。在 nslog 中显示数组对象的值后,我的应用程序崩溃了。
    • 还是一样,虽然我已经按照你提到的[[severity alloc]init]进行了编辑; [严重保留]; [super viewDidLoad] 之后;
    • 没有严重性只是看到你的单元格文本正在打印还是不只是为了测试看到.....cell.textLabel.text = @"this text for cell";如果没有打印那么你的tableview有问题。然后检查你有没有是否完美地调用了您的 tableview 委托和数据源。如果有任何问题,请不要犹豫,问我...
    • 它不打印文本,但如果我使用新的测试数组并明确添加对象然后给出 [test count] 它工作正常.. 如果它的严重性它不进入 rowAtIndexPath..方法
    • 你到底想说什么?
    【解决方案5】:

    我认为在您的代码表重新加载问题。请在收到 webservice 的响应后重新加载表格。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-10
      • 2012-01-06
      • 2013-07-23
      • 1970-01-01
      • 1970-01-01
      • 2015-04-30
      相关资源
      最近更新 更多