简体   繁体   English

UITableView “cellForRowAtIndexPath”方法在突然滑动时被调用两次

[英]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.我可能很多人在UITableView委托方法上遇到过这个问题- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath被调用两次。

In my application I transforming the tableView.在我的应用程序中,我转换了 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.一次可见 1 个单元格,但在应用程序启动时第一次可见。 The log shows visible cells 0.日志显示可见单元格 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) (当我从UIWebView类实现 Apple 委托时,我遇到了完全相同的行为)

EDIT:编辑:

Create a BOOL member in the class header, then in the init set the value to be YES .在 class header 中创建一个BOOL成员,然后在 init 中将值设置为YES So if the BOOL is called mbIsFirstCall for example, in your delegate method, do the following:因此,如果BOOL被称为mbIsFirstCall ,例如,在您的委托方法中,请执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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