[英]Can't reload data in tableView after parsing XML
我正在啟動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];
行上有異常NSLog(@"%@", self.titleArr objectAtIndex:indexPath.row];
- 說:“線程1:EXC_BAD_ACCESS(代碼1)我怎么能理解它意味着它正在嘗試在沒有准備的時候初始化數組。我的問題是我不能延遲tableView方法或者什么?我該如何修復它?我正在嘗試修復它已有幾天了...
你的titleArr從內存中釋放出來然后你正在請求它的對象,所以它會讓你崩潰。 所以通過這種方式將內存分配給數組。
_titleArr = [[NSArray alloc] initWithArray:[[[_xmlDictionary objectForKey:@"result"] objectForKey:@"name"] valueForKey:@"text"]];
盡管不推薦這種方式,但以下內容可以為您提供解決方案
self.tableView.delegate = nil;
self.tableView.datasource = nil;
並且在安全的情況下,您的數組將使用xml數據集填充回來
self.tableView.delegate = self;
self.tableView.datasource = self;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.