2

I have googled and done lot of research from my side to find out why the reloadData method on tableview wouldn't work. I checked all the possible solutions like the datasource is set, delegate is set, the tableview is connected to the file's owner.

After all these, when I am trying to reload the tableview, the no. of rows method gets called, but the cell for rowAtIndexPath doesn't get called. Below is the code that I have written. Please let me know, where I am going wrong

- (void)onReservationListSuccess:(NSArray *)rData
 {
if ( rData != nil )
{
    resList = [[NSArray alloc] initWithArray:rData];

    if([resList count] > 0)
    {
        [self.tripsTableView reloadData];
        //[self.tripsTableView beginUpdates];
        //[self.tripsTableView reloadSections:[NSIndexSet indexSetWithIndex:0]
          //            withRowAnimation:UITableViewRowAnimationNone];
        //[self.tripsTableView endUpdates];
    }
    else
    {
        [tripsTableView reloadData];
        [tripsTableView setHidden:YES];
        [noTripsLabel setHidden:NO];
    }
}

if(fsnNeedsRefresh == YES)
{
    [[NSNotificationCenter defaultCenter] postNotificationName:UpdateFSNList object:nil];
    fsnNeedsRefresh = NO;
}
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
  {
   return 1;
  }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
 {
int temp=[resList count];
NSLog(@"The no. of rows are %d", temp);
NSLog(@"Testing Purpose");
NSLog(@"The pnr details of the object is:%@",((TripData *)[resList objectAtIndex:0]).pnrDescription);
return 1;
 }


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
  NSLog(@"The cell for the row at indexpath is getting called");
static NSString *CellIdentifier = @"TripCellIdentifier";

TripCell *cell = (TripCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) 
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TripCell" owner:self options:nil];

    for(id oneObject in nib)
        if([oneObject isKindOfClass:[TripCell class]])
            cell = (TripCell *)oneObject;
}

// Set up the cell...
TripData *tripData = (TripData *)[resList objectAtIndex:indexPath.row];
cell.pnrLabel.text = tripData.pnr;
NSLog(@"The cell text is %@",tripData.pnr);
cell.pnrDescriptionLabel.text = tripData.pnrDescription;
NSLog(@"The cell text is %@",tripData.pnrDescription);
cell.pnrTypeLabel.text = tripData.pnrType;
NSLog(@"The cell text is %@",tripData.pnrType);

if(checkInAllowed)
{
    cell.checkInButton.tag = indexPath.row;
    [cell.checkInButton addTarget:self action:@selector(checkIn:) forControlEvents:UIControlEventTouchUpInside];
}
else
{
    [cell.checkInButton setEnabled:NO];
}

return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
// Navigation logic may go here. Create and push another view controller
TripData *tripData = (TripData *)[resList objectAtIndex:indexPath.row];

NSLog(@"%@", tripData.pnr);

if(tripData != nil)
{
    TripOverviewViewController *tripOverviewViewController = [[TripOverviewViewController alloc] initWithTrip:tripData];
    [self.navigationController pushViewController:tripOverviewViewController animated:YES];
    [tripOverviewViewController release];
}

[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
jeevangs
  • 306
  • 5
  • 20

1 Answers1

3

From this part of code I cannot say exactly why it does not work but I'll try to explain how reloadData works.

First, how UITableView works: basically, it's a scrollview. When it is drawn, it checks how many rows it has, then checks their height and from its size and scroll position it decides which rows are currently displayed. Then it asks the delegate to return a UITableViewCell for every displayed row. When the table is scrolled, it removes the hidden cells from the view hierarchy and adds the cells that have appeared.

And now the tricky part - what does reloadData do? It just removes all the UITableViewCells from the table hierarchy. Nothing more. The actual update is done when the table is drawn for the first time after reloadData.

So, my suggestion is - check that your table is not hidden and check its frame. Also, I see that you are accessing both a property getter self.tripsTableView and an ivar tripsTableView. This is confusing. Do they both return the same?

Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • Thank u very much for the explanation. I did change the above line to [tripsTableView reloadData] and dint see any changes. I debugged and found that in the very first method, I am storing some result in the array rData, and the reloadData method calls no. of rows, where I am able to print content from rData. But it is not calling cellForRowIndexPath and if i scroll the tableview, there is no data in the array rdata at all. – jeevangs Feb 24 '12 at 21:06
  • The only explanation I see is that the table thinks no rows should be displayed. You are returning 1 from numberOfSections and 1 from numberOfRowsInSection so there should be always 1 row displayed. Is the `rowHeight` property set correctly? Isn't the cell hidden by scrollview insets? – Sulthan Feb 24 '12 at 21:19
  • u r right. Only 1 cell has to be displayed and the cell is visible. No issues with that. My only concern is, the cell for row at indexpath is not getting called properly. anyways, I will try to figure out where I am going wrong. If you come across, please let me know. – jeevangs Feb 24 '12 at 21:43
  • Oh, I understand know. Try to log table subviews before and after calling `reloadData` - `for (UIView* subview in self.tripsTableView){ NSLog(@"Table subview: %@", subview) }` – Sulthan Feb 24 '12 at 21:50
  • I would change one thing in your explanation. `reloadData` tells the tableView to begin the process (described in your first paragraph) from the beginning. `reloadData` tells the tableView togo the the process that includes calling those delegate methods. – Jim Feb 24 '12 at 22:01
  • It says UITableView may not respond to countByEnumeratingWithState:objects – jeevangs Feb 24 '12 at 22:02
  • @Jim - Yes, but the process does not start until the next redraw. The delegate methods are not called immediately. @jeevangs, sorry, meant `for (UView* subview in self.tripTableView.subviews)` – Sulthan Feb 24 '12 at 23:30
  • I did try adding the piece of code that u suggested me, before and after the tableview reload data and logged it. Before: Table subview: > After reloadData: Table subview: > – jeevangs Feb 27 '12 at 15:13