0

我正在启动 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 下载 - OK,解析 - OK,初始化数组 - OK。但是当我开始项目时,我在行上有异常NSLog(@"%@", self.titleArr objectAtIndex:indexPath.row];- 说:“线程 1:EXC_BAD_ACCESS(代码 1)我怎么能理解这意味着它在未准备时正在尝试初始化数组。我的问题是我不能延迟 tableView 方法或什么?我该如何修复它?我正在尝试修复它几天...

4

2 回答 2

2

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

_titleArr = [[NSArray alloc] initWithArray:[[[_xmlDictionary objectForKey:@"result"] objectForKey:@"name"] valueForKey:@"text"]];
于 2013-11-13T10:49:21.110 回答
0

尽管不是推荐的方式,但以下可以为您提供解决方案

self.tableView.delegate = nil;

self.tableView.datasource  = nil;

并且当您的数组安全地填充了 xml 数据集时

self.tableView.delegate = self;

self.tableView.datasource  = self;
于 2013-11-13T10:54:11.043 回答