1

在我的应用程序中,我启动 NSURLConnection,解析 XML,从此 XML 初始化数组,并将其显示在 tableView 中。在 ViewDidLoad 中,我使用查询参数 0 请求服务器,它为我返回字符串,在 tableView 4 行 - 标题中进行所有转换之后,当我推送其中一些标题时,所有进程(连接到服务器,解析、数组初始化、) 必须重复。在 didSelectedRowAtIndexPath 中,我必须传输部分 ID(以便服务器向我发送正确的数据)。我怎样才能正确地做到这一点?我在 ViewDidLoad 中建立连接,如何再次调用它?

我的 .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 = [[NSArray alloc] initWithArray:[[[_xmlDictionary objectForKey:@"result"] objectForKey:@"name"] valueForKey:@"text"]];
_IDArr = [[NSArray alloc] [[[_xmlDictionary objectForKey:@"result"] objectForKey:@"id"] valueForKey:@"text"]];
_priceArr= [[NSArray alloc][[[_xmlDictionary objectForKey:@"result"] objectForKey:@"price"] valueForKey:@"text"]]; 
_ImageURLArr=[[NSArray alloc][[[_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 
4

1 回答 1

0

您可以将相关代码从 viewDidLoad 移动到特定方法,例如:

- (void)loadXMLData {
    // Initiate your loading/parsing 
}

在 viewDidLoad 中只需调用此方法:

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadXMLData];
}

这样,您可以多次调用 [self loadXMLData]。

但是......从 UITableViewDelegate 方法实现中调用 [tableView reloadData] 时要小心,因为这将导致 tableview 调用(至少其中一些)委托方法,这可能会导致递归循环或其他奇怪的行为。

于 2013-11-13T12:07:56.993 回答