2013-01-04 58 views
0

我已經把一個斷點在數據不會增加細胞

Cateogry_Cell * cell = (Cateogry_Cell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

,並發現它總是返回零,我不知道爲什麼那麼它會通過

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

無論是他們是一個我看不見的問題,或者我的代碼在零部分是錯的,但我不是100%投入它的東西。

我正在使用Storyboard,所以我創建了我的單元格,並且沒有通過代碼創建樣式。

  #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. 
       [self.Category_Table registerClass:[Cateogry_Cell class] forCellReuseIdentifier:@"Cateogry_Cell"]; 

       NSString *requestURL = [NSString stringWithFormat:@"http://URL.4fx.co.uk/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]; 

         //   NSDictionary *tempfile = [self.Category_Array objectAtIndex:i - 1]; 
         //   [[[UIAlertView alloc] initWithTitle:@"Added to Cat" message:[tempfile objectForKey:@"Category"] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show]; 

         count++; 
        } 
        @catch (NSException *exception) { 

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


      //#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 = (Cateogry_Cell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

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

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

       //[[[UIAlertView alloc] initWithTitle:@"In Table Function" message:[temp objectForKey:@"Category"] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show]; 

       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

即使在出隊時也使用'@「Cateogry_Cell」'。 –

回答

0

輕微改變:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *cellIdentifier = @"Cateogry_Cell"; 
     Cateogry_Cell * cell = (Cateogry_Cell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

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

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

     //[[[UIAlertView alloc] initWithTitle:@"In Table Function" message:[temp objectForKey:@"Category"] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show]; 

     return cell; 
    } 

這樣做,出列...方法將總是返回零,直到實際上沒有可用於重用的小區。那是當一個單元從屏幕上滾動出來時。 如果您希望此方法始終返回一個單元格,請將該類和標識符一起註冊。你可以調用初始化方法或viewDidLoad:

[self.tableView registerClass:[Category_Cell class] forCellReuseIdentifier:@"Cateogry_Cell"]; 

再次,更好地定義一個常數。

+0

我的表是Category_Table,我的TableView是Category_tableView,標識符是Category_Cell。所以我做了這個[self.Category_Table registerClass:[Category_TableView] forCellReuseIdentifier:@「Cateogry_Cell」];但是我在[Category_TableView] –

+0

之後顯示錯誤預期標識符,特別是在「預期標識符」之前顯示代碼。如果您使用評論,請將其格式設置爲代碼或編輯您的問題。預計的標識符錯誤通常會在此行中引起。不過,我想它應該是'[self.Category_Table registerClass:[Category_Cell class] forCellReuseIdentifier:@「Cateogry_Cell」];'而不是。先嚐試一下。 –

+0

沒有工作。我原來的問題的主要區域現在已經在表格的.m文件中得到了我的完整代碼。謝謝 –