2010-07-08 166 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

相关问题