2011-08-16 60 views
1

我正在處理數據從後端顯示到tableview的應用程序。 問題是,當表加載時,它獲取所有數據,但只顯示每行中數據庫的最後一項。假設最後一部分有3行,那麼它只顯示3表示每個部分有3行數據。即使在某些部分行中有9行,其餘行也顯示爲空白。如何調用numberOfRowsInSection和cellForRowAtIndexPath或者

我發現它通過斷點,表首先從數據庫中獲取所有數據,然後讀取cellForRowAtIndexPath,因爲在我的數據庫中,最後一個表有3行,它僅通過將其餘行保留爲空來顯示3行的數據。

這裏是我的numberOfRowsInSection的COE和的cellForRowAtIndexPath

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 

    if(requestType == 2) 
    {  
     if (self.uniqueCityCount > 0) 
     { 
      NSString *strCity = [self.arrayCityNames objectAtIndex:section];   //@"EventData" :@"EventCity" 
      NSPredicate *predicateSettings = [NSPredicate predicateWithFormat:@"(EventCity = %@)",strCity]; 
      if ([self.arrayEventDescription count]>0) 
      { 
       [self.arrayEventDescription removeAllObjects]; 
      } 
      self.arrayEventDescription = [CoreDataAPIMethods searchObjectsInContext:@"EventData" :predicateSettings :@"EventCity" :YES :self.managedObjectContext]; 
      return [self.arrayEventDescription count]; 
     } 
     /* 
     if (section == 0) 
     { 
     NSString *strCity = [self.arrayCityNames objectAtIndex:section];   //@"EventData" :@"EventCity" 
     NSPredicate *predicateSettings = [NSPredicate predicateWithFormat:@"(EventCity = %@)",strCity]; 
     self.arrayEventDescription = [CoreDataAPIMethods searchObjectsInContext:@"EventData" :predicateSettings :@"EventCity" :YES :self.managedObjectContext]; 
     return [self.arrayEventDescription count]; 
     } 
     else if (section == 1) 
     { 

     }*/ 
    }  
return 0; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{  
NSString *CellIdentifier = [@"" stringByAppendingFormat:@"Cell%d",indexPath.row];  
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

cell.backgroundColor = [UIColor clearColor]; 

if(requestType == 2) 
{ 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

     // for (int j = 0 ; j < [self.arrayEventDescription count]; j++) 

     // for(int j=0; j<[tableView numberOfSections]; j++) 

     //{ 

     NSLog(@"at section %d",indexPath.section); 
     NSLog(@"at row %d",indexPath.row); 
     NSLog(@"array count %d",[self.arrayEventDescription count]); 

     if (indexPath.row < [self.arrayEventDescription count]) 
     { 


      EventData *data = [self.arrayEventDescription objectAtIndex:indexPath.row]; 
      //EventData *data = [self.arrayEventDescription objectAtIndex:j]; 

      cell.selectionStyle=UITableViewCellSelectionStyleNone; 
      cell.backgroundColor=[UIColor clearColor];  


      // UIView *viewDescription = [[UIView alloc]initWithFrame:CGRectMake(00, 00, 480, 35)]; 

      ////////////////////// Labels for description of city events from database ////////////////////////////    

      UILabel *lblEvent = [[UILabel alloc]initWithFrame:CGRectMake(15, 00, 150, 30)]; 
      lblEvent.font = [UIFont systemFontOfSize:12]; 
      lblEvent.backgroundColor = [UIColor clearColor]; 

      UILabel *lblEventAtDate = [[UILabel alloc]initWithFrame:CGRectMake(200, 00, 150, 30)]; 
      lblEventAtDate.font = [UIFont systemFontOfSize:12]; 
      lblEventAtDate.backgroundColor = [UIColor clearColor]; 

      UILabel *lblEventAtSchool = [[UILabel alloc]initWithFrame:CGRectMake(350, 15, 150, 30)]; 
      lblEventAtSchool.font = [UIFont systemFontOfSize:12]; 
      lblEventAtSchool.backgroundColor = [UIColor clearColor]; 

      [cell.contentView addSubview:lblEvent]; 
      [cell.contentView addSubview:lblEventAtDate]; 
      [cell.contentView addSubview:lblEventAtSchool]; 


      lblEvent.text = data.EventName; 
      // lblEventAtDate.text = data.EventDate; 
      lblEventAtSchool.text = data.EventPlace; 
     } 



     //} 
    } 
} 


// Configure the cell... 

return cell; 
} 

回答

0

作爲第一件事情,我都感動了細胞==零塊,因爲重用可能不會發生。看看這是否能解決你的問題。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

{  

NSString *CellIdentifier = [@"" stringByAppendingFormat:@"Cell%d",indexPath.row];  
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
cell.backgroundColor = [UIColor clearColor]; 

if(requestType == 2) 
{ 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

     // for (int j = 0 ; j < [self.arrayEventDescription count]; j++) 

     // for(int j=0; j<[tableView numberOfSections]; j++) 

     //{ 

     NSLog(@"at section %d",indexPath.section); 
     NSLog(@"at row %d",indexPath.row); 
     NSLog(@"array count %d",[self.arrayEventDescription count]); 





     //} 
    } 

    if (indexPath.row < [self.arrayEventDescription count]) 
    { 


     EventData *data = [self.arrayEventDescription objectAtIndex:indexPath.row]; 
     //EventData *data = [self.arrayEventDescription objectAtIndex:j]; 

     cell.selectionStyle=UITableViewCellSelectionStyleNone; 
     cell.backgroundColor=[UIColor clearColor];  


     // UIView *viewDescription = [[UIView alloc]initWithFrame:CGRectMake(00, 00, 480, 35)]; 

     ////////////////////// Labels for description of city events from database ////////////////////////////    

     UILabel *lblEvent = [[UILabel alloc]initWithFrame:CGRectMake(15, 00, 150, 30)]; 
     lblEvent.font = [UIFont systemFontOfSize:12]; 
     lblEvent.backgroundColor = [UIColor clearColor]; 

     UILabel *lblEventAtDate = [[UILabel alloc]initWithFrame:CGRectMake(200, 00, 150, 30)]; 
     lblEventAtDate.font = [UIFont systemFontOfSize:12]; 
     lblEventAtDate.backgroundColor = [UIColor clearColor]; 

     UILabel *lblEventAtSchool = [[UILabel alloc]initWithFrame:CGRectMake(350, 15, 150, 30)]; 
     lblEventAtSchool.font = [UIFont systemFontOfSize:12]; 
     lblEventAtSchool.backgroundColor = [UIColor clearColor]; 

     [cell.contentView addSubview:lblEvent]; 
     [cell.contentView addSubview:lblEventAtDate]; 
     [cell.contentView addSubview:lblEventAtSchool]; 


     lblEvent.text = data.EventName; 
     // lblEventAtDate.text = data.EventDate; 
     lblEventAtSchool.text = data.EventPlace; 
    } 
} 


// Configure the cell... 

return cell; 

}

+0

數據以前面的方式顯示,但是這一次,相同的數據重疊在一起,因此字母由於重疊而以粗體顯示。 –

+0

我可以看到你的CGRect在相同的屏幕座標上反覆創建控件。你能考慮使用自定義表格單元嗎?請參考:http://www.e-string.com/content/custom-uitableviewcells-interface-builder – Saran

0

你能告訴我什麼是請求的類型和什麼時候會被調用(一組可能的值)。

快速解決方案:您正在檢查請求類型的條件,首先是if(requestType == 2) {,然後您正在加載單元。這個委託方法將在加載tableview時被調用,但requesttype是2.所以對於indexpath.row = 0 & 1,它不會進入加載單元格,它只會加載requesttype = 2。所以它正在加載第三行。

+0

RequestType是一個整數。實際上,我有4個子視圖附加到xib視圖。這是我的第二個視圖,因此請求類型2將被調用以顯示第二個視圖,其中我有上面討論的表視圖。 –

+0

RequestType只是一個整數,我已經定義了多少個部分,如果RequestType是2,那麼部分數是數組數。所以我不認爲這個RequestType可以創建任何問題。我使用ReuestType將四個視圖區分爲每個包含一個表的視圖。 –

相關問題