2010-07-08 163 views
0

我有一個UIView,並且我已經放置了一個tableview,tableview使用從NSArray中提供數據的自定義單元格。一些奇怪的事情發生在我最多的時候,我只能讓表格顯示2個單元格,即使有10個單元格,所有10個單元格都已填充。UITableView與自定義單元格

我有一個不同的筆尖排列相同的方式,它的作品完美。

正在發生的事情的截圖(5是10個單元格中的第5個,所有這些單元格都填充了數據)。

http://img692.imageshack.us/img692/1465/screenshot20100708at215.jpg

-(UITableViewCell *)tableView:(UITableView *)tblView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"ISB points cellForRowAtIndexPath start"); 
    NSLog(@"Elements in array = %d",[self.listData count]); 
    // 
    static NSString *cellID = @"PointsCellIdentifier"; 

    PointsCustomCell *cell = [tblView dequeueReusableCellWithIdentifier:cellID]; 

    if(cell == nil) 
    { 
     NSArray *nibObjects = [[NSBundle mainBundle]loadNibNamed:@"PointsCustomCell" owner:nil options:nil]; 

     for(id currentObject in nibObjects) 
     { 
      NSLog(@"nibObjects count = %d", [nibObjects count]); 

      if([currentObject isKindOfClass:[PointsCustomCell class]]) 
      { 
       cell = (PointsCustomCell *)currentObject; 
      }// if 
     }// for 
    }// if 

    if(indexPath.row < [listData count]) 
    { 
     RiderPointsData *riderPts = (RiderPointsData *)[listData objectAtIndex:indexPath.row];  
     cell.posLabel.text = riderPts.pos; 
     cell.nameLabel.text = riderPts.name; 
     cell.pointsLabel.text = riderPts.points; 
     cell.winsLabel.text = riderPts.wins; 
     //[riderPts release];    
    } 

    NSLog(@"ISB points cellForRowAtIndexPath end"); 

    return cell; 
} 
+1

發佈您用於TableView數據源代理的代碼 – Avalanchis 2010-07-08 21:18:41

+0

而不是在評論中發佈代碼,在沒有格式的情況下,使用適當的代碼編輯您的問題。 – 2010-07-08 21:51:01

+0

哪裏是你的[tableView:numberOfRowsInSection:](http://developer.apple.com/iphone/library/documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UITableViewDataSource/tableView: numberOfRowsInSection :)方法?什麼是*它*返回? – progrmr 2010-07-08 22:45:20

回答

0

它看起來像在一些的ListData數據爲NULL。 你如何填充你的listData?我想如果你添加這兩行,讓我們看看控制檯的輸出結果會更好。

//add this line in tableView:cellForRowAtIndexPath: 
NSLog(@"indexPath.row = %d listData.count = %d ",indexPath.row,[listData count]); 
//add this line in if(indexPath.row < [listData count]) 
NSLog(@"RiderPointsData = %@ %@", riderPts,[riderPts class]); 
+0

indexPath.row = 1 listData.count = 10 RiderPointsData = RiderPointsData – Del 2010-07-09 17:34:49

+0

只有一行被觸及?如果你確定你的tableView:numberOfRowsInSection:是正確的。您可以嘗試刪除並重新連接您的桌面視圖插座與IB中的代表和數據源。這個技巧幫助我解決了一些關於tableview的奇怪問題。 – syoleen 2010-07-12 03:50:07

1

它看起來像你遍歷你的筆尖的所有對象,這可能包括的UITableViewCell和4個UILabels它包含的,只有在某些時候是細胞得到正確分配。您不需要nibObjects數組或for for循環。如果您給loadNibNamed一個owner參數,它會將文件所有者設置爲該值。如果您的numberOfRowsInSection方法返回[listData count],您也不需要indexPath.row < [listData count]檢查。因此,改變你的代碼:

-(UITableViewCell *)tableView:(UITableView *)tblView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellID = @"PointsCellIdentifier"; 

    PointsCustomCell *cell = [tblView dequeueReusableCellWithIdentifier:cellID]; 

    if(cell == nil) 
    { 
    [[NSBundle mainBundle]loadNibNamed:@"PointsCustomCell" owner:self options:nil]; 

    // assign IB outlet to cell 
    cell = self.cellOutlet; // cellOutlet should be whatever you name your IBOutlet 
    self.cellOutlet = nil; 

    }// if 

    RiderPointsData *riderPts = (RiderPointsData *)[listData objectAtIndex:indexPath.row];  
    cell.posLabel.text = riderPts.pos; 
    cell.nameLabel.text = riderPts.name; 
    cell.pointsLabel.text = riderPts.points; 
    cell.winsLabel.text = riderPts.wins; 

    return cell; 
} 

和:

  1. 創建控制器一個UITableViewCell出口
  2. 讓你的控制器文件的所有者在PointsCustomCell.xib
  3. 綁定你的UITableViewCell到出口您在步驟1中創建的
+0

謝謝,但當我嘗試你的代碼的應用程序崩潰。 終止應用程序,由於未捕獲異常'NSInternalInconsistencyException',原因:'UITableView dataSource必須從tableView返回一個單元格:cellForRowAtIndexPath: – Del 2010-07-09 17:30:22

+0

很抱歉,更新了代碼。加載該筆尖後,將填充您的UITableViewCell IBOutlet,但仍然需要將其分配給您要返回的本地*單元格指針。 – 2010-07-09 17:47:12

+0

試過了,現在它不會崩潰,只是做了和我自己的代碼完全一樣的事情。即tableview只顯示一個單元 – Del 2010-07-09 18:03:43

相關問題