【问题标题】:IOS How to display NSXMLparser with NSMutable array into tableview?IOS 如何将带有 NSMutable 数组的 NSXMLparser 显示到 tableview 中?
【发布时间】:2016-01-25 05:36:02
【问题描述】:

我有一个 NSXML 解析器,它愿意检索数据并将其放入 NSMutable 数组。它在我 NSLog 数组输出时起作用。但是当我将数组显示到表视图中时它失败了。如果可以更具体地告诉我我遇到了哪个错误,那就太好了,因为运行时没有错误。

这是我的 ViewDidLoad

       - (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");
            }
        }

This is my NSXMLparser

    //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);
        //[self.datalist reloadData];
    }

这是我的桌面视图

- (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;





}

【问题讨论】:

  • 您是否分配了表格视图的数据源?你连接了 table view 的插座吗?
  • 记录这个“cell.textLabel.text”并观察它显示了什么?
  • 是的,我已分配数据源并连接到 IBoutlet。

标签: ios objective-c uitableview nsmutablearray nsxmlparser


【解决方案1】:

查看我完成的这个 XMLParser 示例。

在您的 .h 文件中:

@interface RssViewController : UIViewController<NSXMLParserDelegate,UITableViewDataSource,UITableViewDelegate>
{
    NSString *element;
    NSXMLParser *xml;
    NSMutableDictionary *item;
    NSMutableString *title;
    NSMutableString *link;
    NSMutableString *description;
    NSMutableArray *arrmute;
}
@property (weak, nonatomic) IBOutlet UITableView *tview;

在 .m 文件中:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    arrmute=[[NSMutableArray alloc]init];
    NSURL *url=[[NSURL alloc]initWithString:@"http://news.google.co.in/news?cf=all&hl=en&pz=1&ned=in&topic=tc&output=rss"];
    xml=[[NSXMLParser alloc]initWithContentsOfURL:url];
    xml.delegate=self;
    xml.shouldResolveExternalEntities=YES;
   [xml parse];
}

NSXMLParser 委托方法:-

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

    if ([element isEqualToString:@"item"])
    {
       item=[[NSMutableDictionary alloc] init];
       title=[[NSMutableString alloc] init];
       link=[[NSMutableString alloc] init];
       description=[[NSMutableString alloc] init];
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
   if ([element isEqualToString:@"title"])
   {
      [title appendString:string];
   }
   else if ([element isEqualToString:@"link"])
   {
      [link appendString:string];
   }
   else if ([element isEqualToString:@"description"])
   {
      [description appendString:string];
   }
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
  if ([elementName isEqualToString:@"item"]) {

     [item setObject:title forKey:@"title"];
     [item setObject:link forKey:@"link"];
     [item setObject:description forKey:@"description"];

     [arrmute addObject:[item copy]];
 }
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{    
   [self.tview reloadData];
}

TableView 方法:-

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return arrmute.count;
}

- (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];
    }
    cell.textLabel.text=[[arrmute objectAtIndex:indexPath.row] objectForKey:@"title"];
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
    return cell;
}

希望对你有所帮助...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-19
    • 2012-08-13
    • 2016-01-25
    • 2016-09-12
    • 2023-03-20
    • 1970-01-01
    • 2019-07-14
    • 1970-01-01
    相关资源
    最近更新 更多