简体   繁体   中英

UITableView “cellForRowAtIndexPath” method gets called twice on a swipe abruptly

I could 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 as under:

    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 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.

Any idea or any suggestion how to solve this?

Many thanks in advance.

Regards.

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

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