繁体   English   中英

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

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

我可能很多人在UITableView委托方法上遇到过这个问题- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath被调用两次。

在我的应用程序中,我转换了 tableView。 代码如下:

    CGAffineTransform transform = CGAffineTransformMakeRotation(-M_PI/2);
theTableView.transform = transform;

    theTableView.rowHeight = self.bounds.size.width;

    theTableView.frame = self.bounds;

现在在委托方法中,我正在做几件事:

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

一次可见 1 个单元格,但在应用程序启动时第一次可见。 日志显示可见单元格 0。

很多时候,这个特定的委托方法会被突然调用两次。

任何想法或任何建议如何解决这个问题?

提前谢谢了。

问候。

我认为立即修复只是设置一个标志,它在第一次被击中时会改变,所以你忽略第二次调用。 这可能不是完美的解决方案,我不能告诉你为什么它会被击中两次——但这会奏效。 (当我从UIWebView类实现 Apple 委托时,我遇到了完全相同的行为)

编辑:

在 class header 中创建一个BOOL成员,然后在 init 中将值设置为YES 因此,如果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