2013-01-03 64 views
0

我正在使用NSUrlConnection轉到.ashx文件來執行MYSQL語句並返回結果。然後放在桌子上。connectionDidFinishLoading其他函數之後不要運行?

在這個特殊的一個我得到類別,這是工作正常,並獲得它應該的細節,但在connectionDidFinishLoading函數沒有其他事情發生後。

我把斷點放在它後面的任何地方,但沒有任何反應,它只是在它進入表格之前結束。

以下是我的代碼,如果您需要我錯過的任何信息,請詢問。

.h文件中

#import <UIKit/UIKit.h> 

    @interface Category_TableView : UIViewController <NSURLConnectionDelegate> 
    { 
     sqlite3 *database; 
    } 
    @property (retain, nonatomic) NSMutableArray *Category_Array; 
    @property (strong, nonatomic) IBOutlet UITableView *Category_Table; 
    @property (retain, nonatomic) NSString *type; 
    @property (retain, nonatomic) NSString *Category; 


    @property (retain, nonatomic) NSMutableData *CatData; 

    @end 

.m文件

  #import "Category_TableView.h" 
      #import "Cateogry_Cell.h" 
      #import "JobList_Table.h" 
      #import "JobList_Cell.h" 
      #import <QuartzCore/QuartzCore.h> 

      @interface Category_TableView() 

      @end 

      @implementation Category_TableView 

      - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
      { 
       self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
       if (self) { 
        // Custom initialization 


       } 
       return self; 
      } 

      - (void)viewDidLoad 
      { 
       [super viewDidLoad]; 
       self.navigationController.navigationBar.hidden = YES; 
       // Do any additional setup after loading the view. 

       NSString *requestURL = [NSString stringWithFormat:@"http://"URL"/forms/AppForms/app_latest.ashx"]; 
       NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: requestURL]]; 
       NSURLConnection *con =[[NSURLConnection alloc] initWithRequest:request delegate:self]; 

       if(con) 
       { 
        self.CatData = [[NSMutableData alloc] init]; 
        self.Category_Array = [[NSMutableArray alloc] init]; 
       } 
       else 
       { 
        [[[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to get connection. Please try again later" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show]; 
       } 

      } 
      - (void)didReceiveMemoryWarning 
      { 
       [super didReceiveMemoryWarning]; 
       // Dispose of any resources that can be recreated. 
      } 
      - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
      { 
       [self.CatData setLength:0]; 
      } 

      - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
      { 
       [self.CatData appendData:data]; 
      } 
      - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
      { 

       [[[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to get connection. Please try again later" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show]; 


      } 
      - (void)connectionDidFinishLoading:(NSURLConnection *)connection 
      { 
       NSString *temp = [[NSString alloc] initWithData:self.CatData encoding:NSUTF8StringEncoding]; 
       NSArray *jobs = [temp componentsSeparatedByString:@"||||"]; 
       NSMutableDictionary *temp_dict = [[NSMutableDictionary alloc] init]; 
       NSArray *jobInfo = [[NSArray alloc] init]; 
       int count = 0; 
       for(int i = 1; i < [jobs count]; i++) 
       { 
        jobInfo = [[jobs objectAtIndex:i] componentsSeparatedByString:@"||"]; 
        @try { 

         temp_dict = [[NSMutableDictionary alloc]init]; 
         [temp_dict setObject:[jobInfo objectAtIndex:0] forKey:@"Category"]; 
         [self.Category_Array addObject:temp_dict]; 
         count++; 
        } 
        @catch (NSException *exception) { 

         [[[UIAlertView alloc] initWithTitle:@"Exception Thrown" message:exception.reason delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show]; 
        } 
       } 
      } 


      //#pragma mark - Table view data source 


      - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
      { 

       // Return the number of sections. 
       return 1; 
      } 

      - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
      { 
       // Return the number of rows in the section. 
       return [self.Category_Array count]; 
      } 


      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
      { 
       static NSString *CellIdentifier = @"Cateogry_Cell"; 
       Cateogry_Cell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
       // Configure the cell... 


       [[[UIAlertView alloc] initWithTitle:@"In Table Function" message:@"Failed to try again later" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show]; 

       if(cell == nil) { 
        cell = [[Cateogry_Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cateogry_Cell"]; 
       } 

       NSDictionary *temp = [self.Category_Array objectAtIndex:[indexPath row]]; 
       cell.Category_Cell_Label.text = [temp objectForKey:@"Category"]; 

       return cell; 
      } 

      -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
      { 
       if ([[segue identifier] isEqualToString:@"Cat_Cell_Push"]) 
       { 
        JobList_Table *DetailController = [segue destinationViewController]; 
        NSIndexPath *MyIndexPath = [_Category_Table indexPathForSelectedRow]; 
        int row = [MyIndexPath row]; 
        DetailController.JobList_Array = @[_Category_Array[row]]; 


       } 
      } 

      @end 

回答

0

問題是NSURLConnection將異步工作。所以當你得到數據時,你的tableview已經被填充了沒有數據。所以,你需要重新加載tableview中,只需添加如下因素代碼的connectionDidFinishLoading:

[Category_Table reloadData]; 
+0

的最後一行我說這是在功能和沒有變化的最後一行。它擊中[Self.Category_Table reloadData];但那就是它。 –

+0

@pateman:你是否將你的表視圖的數據源和委託設置爲self? –

+0

驚人的工作......但你可以看看錶部分,因爲它現在沒有添加到表! –

相關問題