0

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;
}
4
  • 1
    Post the code you're using for the TableView Data Source delegate Commented Jul 8, 2010 at 21:18
  • Instead of posting the code in a comment, where there is no formatting, edit your question with the appropriate code. Commented Jul 8, 2010 at 21:51
  • Where's your tableView:numberOfRowsInSection: method? What is it returning? Commented Jul 8, 2010 at 22:45
  • in the view controller class and it returns the count of the array i.e. return [self.listData count]; Commented Jul 9, 2010 at 19:16

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

7 Comments

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:
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.
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
anyone know? I'm now thinking that maybe its just an interface builder issue?
if i select cell one in the tableview, it now shows cell 1 and cell 2 img714.imageshack.us/img714/2996/screenshot20100710at125.jpg
|
0

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]);

2 Comments

indexPath.row = 1 listData.count = 10 RiderPointsData = <RiderPointsData: 0x3c0b2b0> RiderPointsData
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.