解析XML后无法在tableView中重新加载数据

时间:2013-11-13 10:45:00

标签: ios objective-c xml uitableview

我正在启动NSURLConnection,解析XML,从这个XML初始化数组,并在tableView中显示它。在connectionDidFinishLoading我正在尝试[self.tableView reloadData,但它不起作用。这是我的代码:

我的.h文件:

@interface catalogViewController : UITableViewController // 
@property (nonatomic, strong) NSMutableData *receivedData;
@property (nonatomic,retain) NSArray * titleArr;
@property (nonatomic,retain) NSArray * contentArr; 
@property (nonatomic,retain) NSArray * ImageURLArr;
@property (nonatomic,retain) NSArray * dateArr;
@property (nonatomic,retain) NSArray * priceArr;
@property (nonatomic,retain) NSDictionary * xmlDictionary; 
 @property (nonatomic,retain) NSArray * IDArr; 
@property (nonatomic) BOOL * didDataLoaded;
@property (strong, nonatomic) IBOutlet UITableView *myTableView;
@end 

我的.m文件:

#import "catalogViewController.h"
#import "XMLReader.h"
@interface catalogViewController () 
@end 


@implementation catalogViewController 

- (id)initWithStyle:(UITableViewStyle)style { 
self = [super initWithStyle:style]; 
if (self) { } return self; 
} 

//-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-CONNECTIONS METHOD START-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[_receivedData setLength:0];
}

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

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[connection release]; 
[_receivedData release];
 NSString *errorString = [[NSString alloc] initWithFormat:@"Connection failed! Error - %@ %@ %@", [error localizedDescription], [error description], [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]]; NSLog(@"%@",errorString);
[errorString release];
}

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-GET FULL DATA HERE-=-=-=-=-=-=-=-=--=-=-=-=-=-=- 
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
 NSString *dataString = [[NSString alloc] initWithData:_receivedData encoding:NSUTF8StringEncoding]; 

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-XMLPARSER PART START-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
NSString *testXMLString = [NSString stringWithContentsOfURL:myURL usedEncoding:nil error:nil];

// -=-=-=-=-=-=-=-=-=-=Parse the XML into a dictionary-=-=-=-=-=-=-=-=-=-=
NSError *parseError = nil; 
_xmlDictionary = [XMLReader dictionaryForXMLString:testXMLString error:&parseError];
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-XMLPARSER PART END-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
_titleArr =[[[_xmlDictionary objectForKey:@"result"] objectForKey:@"name"] valueForKey:@"text"]; 
_IDArr =[[[_xmlDictionary objectForKey:@"result"] objectForKey:@"id"] valueForKey:@"text"];
_priceArr=[[[_xmlDictionary objectForKey:@"result"] objectForKey:@"price"] valueForKey:@"text"]; 
_ImageURLArr=[[[_xmlDictionary objectForKey:@"result"] objectForKey:@"img"] valueForKey:@"text"]; 

 [connection release];
[_receivedData release];
[dataString release]; 

_didDataLoaded=TRUE;

[_myTableView reloadData]; // IBOutlet property 
[self.tableView reloadData]; //default

} 

//-=-=-=-=-=-=-=-=-=-=-Connection methods END-=-=-=-=-=-=-=-=-=-
- (void)viewDidLoad {
[super viewDidLoad]; 

_didDataLoaded=FALSE; 

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-XMLPARSER PART START-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//-=-==-=-=-=-=-=-=-=-=-=-=--=-=START Shit with connection-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=
 NSString* params = @"request_params";
 NSURL* url = [NSURL URLWithString:@"my URL"];
 NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15.0];
[request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 
request.HTTPMethod = @"POST";
request.HTTPBody = [params dataUsingEncoding:NSUTF8StringEncoding]; 
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

if (connection) {
NSLog(@"Connecting...");
_receivedData = [[NSMutableData data] retain]; 
} else {
NSLog(@"Connecting error"); 
}
} 

//-=-==-=-=--=-==-=-=-=-=-=--=-==---=-=--==-=-=-=-=-TableView methods-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=--=-=-=-=-=-=-=
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (_didDataLoaded == FALSE) {
return 1;
} 
else return self.titleArr.count; 
} 

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

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"creatures"]; 
UIImage *creatureImage = nil; 

if (_didDataLoaded == FALSE) {
cell.textLabel.text=@"Downloading...";
cell.detailTextLabel.text= @"downloading...";
} else {
 cell.textLabel.text = [self.titleArr objectAtIndex:indexPath.row]; 
cell.detailTextLabel.text= _IDArr[indexPath.row]; 
NSString *img = self.ImageURLArr[indexPath.row];
creatureImage =[[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:img]]]; cell.imageView.image = creatureImage; 
} 
return cell; 
}

 @end 

XML下载 - 好的,解析 - 好的,初始化数组 - 好的。但是当我开始项目时,我在行NSLog(@"%@", self.titleArr objectAtIndex:indexPath.row];上有例外 - 说:“线程1:EXC_BAD_ACCESS(代码1) 我怎么能理解它意味着它在没有准备时尝试初始化数组。我的问题是我不能延迟tableView方法或什么?我该如何解决?我正在尝试解决它... ...

2 个答案:

答案 0 :(得分:2)

您的titleArr已从内存中释放,然后您正在请求它的对象,因此它会让您崩溃。所以通过这种方式将内存分配给数组。

_titleArr = [[NSArray alloc] initWithArray:[[[_xmlDictionary objectForKey:@"result"] objectForKey:@"name"] valueForKey:@"text"]];

答案 1 :(得分:0)

尽管不推荐这种方式,但以下内容可为您提供解决方案

self.tableView.delegate = nil;

self.tableView.datasource  = nil;

当安全地将数组填充xml数据集回到

self.tableView.delegate = self;

self.tableView.datasource  = self;