如何使用didSelectedRowAtIndexPath在tableView中重载数据并调用其中的方法组

在我的应用程序,我开始NSURLConnection,parsingXML,从这个XML初始化数组,并显示在tableView中。 在ViewDidLoad我呼吁服务器与查询参数0,它返回给我的string,所有转换后在tableView 4行 – 标题,当我推这些标题,所有进程(连接到服务器,parsing,数组初始化)必须重复。 在didSelectedRowAtIndexPath我必须传输节ID(以便服务器给我发送正确的数据)。 我怎样才能正确地做到这一点? 我在ViewDidLoadbuild立连接,我怎么能再次调用它?

我的.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 

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

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

在viewDidLoad中调用这个方法:

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

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

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