简体   繁体   中英

UITableView with Custom cell

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM