views:

104

answers:

2

Hi Guys

I have a UIView and on it i have place a tableview, the tableview uses custom cells fed data from an NSArray. Some strangeness going on in that at most I can only ever get the table to display 2 cells even though there are 10 cells there and all 10 are populated.

I have a different nib arranged the same way and it works perfect.

Screenshot of what is happening (5 is the 5th of 10 cells all of which are populated with data).

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;
}
A: 

It looks like that some data in listData is NULL. How do you populate your listData? I guess it's better if you add these two lines and let's see the console output.

//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]);
syoleen
indexPath.row = 1 listData.count = 10RiderPointsData = <RiderPointsData: 0x3c0b2b0> RiderPointsData
Del
There is only ONE row which was reached? If you are sure your tableView:numberOfRowsInSection: is correct. You can try to delete and re-connect your tableview outlets with its delegate and datasource in IB. This trick helped me solve some strange problems about tableview.
syoleen
+1  A: 

It looks like you're iterating over all the objects in your nib, which probably include the UITableViewCell and 4 UILabels it contains, and only some of the time is cell getting assigned correctly.

You don't need the nibObjects array or your for loop. If you give loadNibNamed an owner parameter, it will set the File's Owner to that value. You also don't need the indexPath.row < [listData count] check if your numberOfRowsInSection method returns [listData count]. So change your code to:

-(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;
}

and:

  1. Create a UITableViewCell outlet in your controller
  2. Make your controller the File's Owner in PointsCustomCell.xib
  3. Bind your UITableViewCell to the outlet you created in step 1
chrispix
Thanks but when I tried your code the app crashes.Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:
Del
Sorry, updated the code. After you load the nib, your UITableViewCell IBOutlet is populated, but you still need to assign that to the local *cell pointer you're returning.
chrispix
Tried that, now it doesn't crash but just does the exact same thing as my own code was doing. i.e. tableview displays only one cell
Del
anyone know?I'm now thinking that maybe its just an interface builder issue?
Del
if i select cell one in the tableview, it now shows cell 1 and cell 2http://img714.imageshack.us/img714/2996/screenshot20100710at125.jpg
Del
You're going to have to troubleshoot and provide some more information about what's actually going on in your code. Add some nil checks and log statements to see if your variables are getting assigned correctly for each cell. Maybe post your updated code, and the rest of your table view delegate code.
chrispix
All cells are there and correctly populated with the correct data, its just that only one cell is visible.if you scroll the table up so that an invisible cell scrolls off the view and the tableview bounces back, that invisible cell now becomes visible and the previously visible cell now becomes invisible.
Del