【问题标题】:iOS Fail to display NSMutableArray to TableViewiOS 无法将 NSMutableArray 显示到 TableView
【发布时间】:2016-01-25 02:24:52
【问题描述】:

下面是我的 .m 文件。我已经在 .h 文件中声明了NSMutableArray,但它仍然无法将我的数组从 xmlparser 显示到UITableView。请通过下面的代码指导我。很可能是我的表格视图代码,因为我能够从提要数组中注销数据。我也重新加载了表格视图控制器。

- (void)viewDidLoad {
    [super viewDidLoad];
   // output = [[NSMutableArray alloc]initWithObjects:@"red",@"green", nil];
    NSLog(@"Response recieved");
    output= [[NSMutableArray alloc] init];
    feeds = [[NSMutableArray alloc] init];
//    NSString *severity = @"Informational";
    NSString *soapMessage = @"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
    "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                             "<soap:Body>"
                            " <IncidentGetList xmlns=\"https://www.monitoredsecurity.com/\">"
                             "<Severity></Severity>"
                             "<SourceOrganization></SourceOrganization>"
                             "<DestinationOrganization></DestinationOrganization>"
                            "<MaxIncidents></MaxIncidents>"
                             "<SourceIP></SourceIP>"
                             "<Category></Category>"
                             "<ExcludeCategory></ExcludeCategory>"
                             "<StartTimeStampGMT></StartTimeStampGMT>"
                             "<EndTimeStampGMT></EndTimeStampGMT>"
                             "<CustomerSeverity></CustomerSeverity>"
                             "</IncidentGetList>"
                             "</soap:Body>"
                             "</soap:Envelope>";


    NSURL *url = [NSURL URLWithString:@"https://api.monitoredsecurity.com/SWS/incidents.asmx"];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];

    [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [theRequest addValue: @"https://www.monitoredsecurity.com/IncidentGetList" forHTTPHeaderField:@"SOAPAction"];
    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];







    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    [connection start];

    if(connection)
    {
        webResponseData = [NSMutableData data] ;

    }
    else
    {
        NSLog(@"Connection is NULL");
    }

       // Menu View




}

















//Implement the NSXmlParserDelegate methods
-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:
(NSString *)qName attributes:(NSDictionary *)attributeDict
{
  element = elementName;

    if ([element isEqualToString:@"SecurityIncidentSummary"]) {

        IncidentGetList    = [[NSMutableDictionary alloc] init];

        Severity   = [[NSMutableString alloc] init];

    }    //NSLog(@"current element: ", elementName);
}
- (void)parserDidStartDocument:(NSXMLParser *)parser{
    NSLog(@"File found and parsing started");

}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if ([element isEqualToString:@"Severity"]) {
        [Severity appendString:string];
    }
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if ([elementName isEqualToString:@"SecurityIncidentSummary"]) {

        [IncidentGetList setObject:Severity forKey:@"Severity"];

        [feeds addObject:[IncidentGetList copy]];
    }
[self.datalist reloadData];
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {

   NSLog(@"Final Feed : %@",feeds);

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
    return [feeds count];

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


static NSString *cellId = @"UITableViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if(cell == nil){
        cell= [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];


    }

   cell.textLabel.text= [[feeds objectAtIndex:indexPath.row]objectForKey:@"Severity"];
    //cell.textLabel.text= output[indexPath.row];
    return cell;





}

//UNTIL HERE


//
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
////    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
//    static NSString *cellIdentifier = @"cell";
//    
//    UITableView *cell = [tableView dequeueReusableCellWithIdentifier:[cellIdentifier ];
//                         
//                         if(cell == nill){
//                             cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]autorelease];
//                         
//                         }
//
//                         
////    
////    return cell;
//}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark - Table view delegate

 //In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    // Navigation logic may go here, for example:
//    // Create the next view controller.
//   // <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:<#@"Nib name"#> bundle:nil];
//    
//    // Pass the selected object to the new view controller.
//    
//    // Push the view controller.
//    //[self.navigationController pushViewController:detailViewController animated:YES];
//    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
//
//}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

下面是我的 nsxmlparser 日志数据

2015-10-26 13:15:28.959 NewsFeeder[1327:634231] parsing result = 0

【问题讨论】:

  • 请不要发布所有代码。将其缩小到与问题相关的相关部分。请用一些细节更新您的问题。不要只是发布一堆代码并要求“为我解决这个问题”。首先使用一些调试来缩小问题范围。
  • 代码很好,但我不知道我错过了哪个部分导致我的表格视图无法显示结果,因此我粘贴了我的整个代码

标签: ios objective-c uitableview nsmutablearray nsxmlparser


【解决方案1】:

为您的 UITableView 创建一个 IBOutlet 并将其命名为 tableView 或其他任何名称,然后在您的以下方法中对其调用 reloadData 方法:

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if ([elementName isEqualToString:@"SecurityIncidentSummary"]) {

        [IncidentGetList setObject:Severity forKey:@"Severity"];

        [feeds addObject:[IncidentGetList copy]];
    }
    [tableView reloadData];
}

【讨论】:

  • 以上是更新后的代码,我添加了我的 tableview 重新加载但它仍然无法加载它。请帮忙。
  • 你能不能在[tableView reloadData]; 行放一个断点并确认feeds 数组有值。
  • 是的,当我 NSLog 提要数组时,里面有值
  • 还要确保您已将dataSource 代表指定为您的UIViewController。如果 feed 有值并且有 reloadData 方法,则放置断点以确认值 tableview:cellForRowAtIndexPath: 方法被调用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多