2

我有一个通过解析 xml 文件填充的UITableView用途。NSMutableArray

问题:数据被添加到数组中,但在UITableView.

子列表.h

@interface SubList:UIViewController <UITableViewDelegate, UITableViewDataSource>
{
    IBOutlet UITableView *List1;
    NSXMLParser *xmlparser;
    NSURLConnection *conn;
    NSMutableData *webdata;
    NSString *FolderID;
    NSMutableArray *info;
    NSMutableArray *SubFolderData;
}

@property(nonatomic,retain) NSMutableArray *info;
@property(nonatomic,retain) NSMutableArray *SubFolderData;
@property (nonatomic,retain)NSString *FolderID;
@property(nonatomic,weak)IBOutlet UITableView *List1;
@property (nonatomic,retain) NSString *theXML;

@结尾

子列表.m

@implementation SubList

- (void)viewDidLoad 
{
    self.navigationController.navigationBar.hidden=YES; 
    [super viewDidLoad];
    [self initWithData];
    [self GetChilds:FolderID UserID:@"1"];
}

-(void)GetChilds:(NSString*)FolderIDRecvd UserID:(NSString*)userID
{
    FolderID=[NSString stringWithFormat:@"%@", FolderIDRecvd];
    NSString *_UserID=@"1";

    NSString *soapMsg=[NSString stringWithFormat:@"<?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><TreeDataSubFolder xmlns='http://tempuri.org/'><FolderID>%@</FolderID><UserId>%@</UserId></TreeDataSubFolder></soap:Body></soap:Envelope>",FolderID,_UserID];


    NSURL *url=[NSURL URLWithString:@"http://192.168.1.5/interlogicsmobile/interlogics.asmx?op=TreeDataSubFolder"];
    xmlparser = [[NSXMLParser alloc] initWithContentsOfURL:url];
    NSMutableURLRequest *req=[NSMutableURLRequest requestWithURL:url];
    NSString *msgLength=[NSString stringWithFormat:@"%d",[soapMsg length]];

    [req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [req addValue:@"http://tempuri.org/TreeDataSubFolder" forHTTPHeaderField:@"SOAPAction"];
    [req addValue:@"length" forHTTPHeaderField:@"Content-Length"];
    [req setHTTPMethod:@"POST"];
    [req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];

    conn=[[NSURLConnection alloc]initWithRequest:req delegate:self];

    if(conn) {
        webdata=[[NSMutableData data] retain];
    }
}

-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *)   response 
{
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response; 
    int responseStausCode = [httpResponse statusCode]; 
    [webdata setLength: 0]; 
}

-(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data 
{
    [webdata appendData:data];
}

-(void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error 
{
    [webdata release];
    [connection release];
}

-(void) connectionDidFinishLoading:(NSURLConnection *) connection 
{
    theXML= [[NSString alloc] 
          initWithBytes: [webdata mutableBytes] 
          length:[webdata length] 
          encoding:NSUTF8StringEncoding];

    [theXML release];

    SubListParser *parserObjForData1=[[SubListParser alloc]init];
    SubFolderData=[[[NSMutableArray alloc]init]retain];
    SubFolderData=[parserObjForData1 UserXMLParser1:webdata];

    [connection release];
    [webdata release];
    [List1 reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return([SubFolderData count]/2);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    int row = [indexPath row]*2;
    static  NSString *cellIdentifier=@"cell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell==nil)
    {
        cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault   reuseIdentifier:@"cell"] autorelease];
        cell.textLabel.text = [SubFolderData objectAtIndex:row+1];
        cell.detailTextLabel.text = [SubFolderData objectAtIndex:row];
    }

       for (int i=0; i<=[SubFolderData count]; i++) {

        NSString *filename = [SubFolderData objectAtIndex:indexPath.row];
        NSString *ext = [filename pathExtension];
        if ([ext isEqualToString:@"doc"] || [ext isEqualToString:@"DOC"]) {
            [cell.imageView setImage:[UIImage imageNamed:@"DOC.png"]];
        } else if ([ext isEqualToString:@"jpg"]|| [ext isEqualToString:@"JPG"]) {
            [cell.imageView setImage:[UIImage imageNamed:@"JPG.png"]];
        }else if([ext isEqualToString:@"pdf"]|| [ext isEqualToString:@"PDF"]){
            [cell.imageView setImage:[UIImage imageNamed:@"PDF.png"]];
        }else if([ext isEqualToString:@""]){
            [cell.imageView setImage:[UIImage imageNamed:@"Folder2.png"]];
        }else if([ext isEqualToString:@"docx"]|| [ext isEqualToString:@"DOCX"]){
            [cell.imageView setImage:[UIImage imageNamed:@"DOCX.png"]];
        }else if([ext isEqualToString:@"TIF"]|| [ext isEqualToString:@"tif"]){
            [cell.imageView setImage:[UIImage imageNamed:@"TIFF.png"]];
        }else if([ext isEqualToString:@"png"]|| [ext isEqualToString:@"PNG"]){
            [cell.imageView setImage:[UIImage imageNamed:@"PNG.png"]];
        }else if([ext isEqualToString:@"gif"]|| [ext isEqualToString:@"GIF"]){
            [cell.imageView setImage:[UIImage imageNamed:@"GIF.png"]];
        }else if([ext isEqualToString:@"jpeg"]|| [ext isEqualToString:@"JPEG"]){
            [cell.imageView setImage:[UIImage imageNamed:@"JPG.png"]];
        }
    }

    return cell;
}

@end

编辑

-(NSMutableArray *)UserXMLParser1:(NSData *)WebDataRecieved1
{
    NSXMLParser *xmlparser1=[[NSXMLParser alloc]initWithData:WebDataRecieved1];
    [xmlparser1 setDelegate:self];
    BOOL success=[xmlparser1 parse];
    NSLog(@"%i",success); return listset1;
}
4

4 回答 4

1

嗨 RANA,我们不知道您是如何进行解析的,因为您没有发布任何解析器方法。但根据你的代码,我得到了一些东西

  1. 此 [parserObjForData1 UserXMLParser1:webdata] 的返回类型;数组是正确的吗?

  2. 如果我的猜测是正确的试试这个

SubListParser *parserObjForData1=[[SubListParser alloc]init];

SubFolderData=[[NSMutableArray alloc]initWithArray:[parserObjForData1 UserXMLParser1:webdata]];

代替

 SubListParser *parserObjForData1=[[SubListParser alloc]init];
    SubFolderData=[[[NSMutableArray alloc]init];
    SubFolderData=[parserObjForData1 UserXMLParser1:webdata];

我希望它对你有帮助。

于 2013-04-12T11:13:04.487 回答
1

你的代表没有被输入,所以你没有设置这些,使用;

[List1 setDelegate:self];
[List1 setDataSource:self];

viewDidLoad

于 2013-04-11T05:57:10.533 回答
1

这是 sublistparser.m 文件

#import "SubListParser.h"
 #import "DocumentManagementAppDelegate.h"

 @implementation SubListParser
  @synthesize mParserArray,ID,URL,SubjectType,Name;
  static int count = 1;


   -(NSMutableArray *)UserXMLParser1:(NSData *)WebDataRecieved1{

NSXMLParser *xmlparser1=[[NSXMLParser alloc]initWithData:WebDataRecieved1];
[xmlparser1 setDelegate:self];
BOOL success=[xmlparser1 parse];
NSLog(@"%i",success);
return listset1;

  }
   - (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
NSLog(@"NSXMLParser ERROR: %@ - %@",  [parseError localizedDescription], [parseError   localizedFailureReason]);
 }

  - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
   namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
   attributes:(NSDictionary *)attributeDict 
 {  
currentElementValue =[elementName copy];
if([elementName isEqualToString:@"FileFolderDetails"]){
    item=[[NSMutableDictionary alloc]init];
   Name=[[NSMutableString alloc]init];
    SubjectType=[[NSMutableString alloc]init];
    URL=[[NSMutableString alloc]init];
   // NSLog(@"%@",Name);
}


//if ([elementName isEqualToString:@"FileFolderDetails"]) {
//  NSLog(@"Inside FolderName");
  //  SUBLIST=[[SubListParser alloc]init];
  //  SUBLIST=[attributeDict objectForKey:@"Name"]; 

//}
 }

 - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//NSLog(@"%@",string);
if(count){
    listset1=[[NSMutableArray alloc]initWithCapacity:20];
    count=0;
    Urlarray = [[NSMutableArray alloc]initWithCapacity:20];
}


if ([currentElementValue isEqualToString:@"Name"]) {
    [Name appendString:string];
    [listset1 addObject:[NSString stringWithFormat:@"%@",Name]];
        //    NSLog(@"%@",listset1);
}

if ([currentElementValue isEqualToString:@"URL"]) {
    [URL appendString:string];
    [Urlarray addObject:[NSString stringWithFormat:@"%@",URL]];
    NSLog(@"Url Array :%@",Urlarray);

}


 }
  - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName;
{
if([elementName isEqualToString:@"FileFolderDetails"]){
[item setObject:Name forKey:@"Name"];
[item setObject:SubjectType forKey:@"Type"];
[item setObject:URL forKey:@"URL"];
[users addObject:item];
}


}


  @end
于 2013-04-15T12:24:44.963 回答
0
  • 在界面构建器中选择您的表格视图
  • 选择连接检查器
  • 连接数据源并委托给文件所有者..

您的表格视图连接应如下图所示。

在此处输入图像描述

添加还检查是否在调用表视图委托之前添加了数组。

于 2013-04-11T06:18:59.917 回答