UITableView中的麻烦

时间:2013-04-11 05:16:06

标签: ios xml uitableview

我有一个UITableView使用NSMutableArray来解析xml文件。

问题:数据已添加到数组中,但在UITableView中不可见。

Sublist.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;

@end

Sublist.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 个答案:

答案 0 :(得分:1)

您没有输入代表,因此您没有设置,使用;

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

中的

答案 1 :(得分:1)

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

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

  2. 如果我的猜测是正确的,请尝试

  3. 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];
    

    我希望它会对你有所帮助。

答案 2 :(得分: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

答案 3 :(得分:0)

  • 在界面构建器
  • 中选择表格视图
  • 选择连接检查器
  • 将数据源和委托连接到文件所有者..

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

enter image description here

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