0

I think many of us has faced this problem on UITableView delegate method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath which gets called twice.

In my application I transforming the tableView. The code is:

    CGAffineTransform transform = CGAffineTransformMakeRotation(-M_PI/2);
theTableView.transform = transform;
    
    theTableView.rowHeight = self.bounds.size.width;

    theTableView.frame = self.bounds;

Now inside the delegate method I am doing a couple of things:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath 
{        
    
    modelRef.currentCellAtIndexPathRow = indexPath.row;
    
    static NSString *CellIdentifier = @"Cell";
    
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            
    if (cell == nil) 
    {
        cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier frame:self.bounds] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
                
    modelRef.currentPageIndex = (indexPath.row + 1);
    
    [cell showPage];

    NSLog(@" visible cell %i ",[[tableView visibleCells] count]);

        
    return cell;
}

At a time 1 cell is visible, but first time when the application launches. The log shows visible cells 0.

Many a times this particular delegate method gets called twice abruptly.

How can I solve this?

halfer
  • 19,824
  • 17
  • 99
  • 186
TechBee
  • 1,897
  • 4
  • 22
  • 46

1 Answers1

0

I think an immediate fix is just to set a flag which changes the first time it is hit, so then you ignore the second call. It's probably not the perfect solution, and I can't tell you why it gets hit twice - but this will work. (I have experienced exactly the same behavior when I implemented an Apple delegate from the UIWebView class)

EDIT:

Create a BOOL member in the class header, then in the init set the value to be YES. So if the BOOL is called mbIsFirstCall for example, in your delegate method, do the following:

if (mbIsFirstCall)
{
    // do your processing, then the line below
    mbIsFirstCall = NO;
}
else
{
    // you don't need this else, but just for clarity it is here.
    // you should only end up inside here when this method is hit the second time, so we ignore it.
}
Rishil Patel
  • 1,977
  • 3
  • 14
  • 30
Luke
  • 11,426
  • 43
  • 60
  • 69
  • Thanks. Can u pls give a sample of code exactly what you mean? – TechBee Jun 03 '11 at 08:11
  • 1
    Thanks again. I tried this earlier. In this scenario, on first swipe,mbIsFirstCall will set to NO. This is one time check I think, is the variable changes to YES anytime? Otherwise on each time else statement will be met. Correct me if I am wrong. – TechBee Jun 03 '11 at 08:59
  • 1
    @iWasRobot If you're happy with the answer, remember to vote up or accept it. :) Glad it has worked out for you! – Luke Jun 03 '11 at 09:02