Objective c UITableView“;cellForRowAtIndexPath“;方法被突然调用两次

Objective c UITableView“;cellForRowAtIndexPath“;方法被突然调用两次,objective-c,ios,ios-4.2,Objective C,Ios,Ios 4.2,我可以说,我们中的许多人都在UITableView委托方法-(UITableViewCell*)tableView:(UITableView*)tableView cell for rowatinexpath:(nsindepath*)indepath上遇到过这个问题,该方法会被调用两次 在我的应用程序中,我将转换tableView。守则如下: CGAffineTransform transform = CGAffineTransformMakeRotation(-M_PI/2); th

我可以说,我们中的许多人都在
UITableView
委托方法
-(UITableViewCell*)tableView:(UITableView*)tableView cell for rowatinexpath:(nsindepath*)indepath
上遇到过这个问题,该方法会被调用两次

在我的应用程序中,我将转换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委托时,我经历了完全相同的行为)

编辑:

在类标题中创建一个
BOOL
成员,然后在init中将值设置为
YES
。因此,例如,如果在委托方法中调用了
BOOL
,请执行以下操作:

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

谢谢你能给我一个你想说的代码样本吗?再次感谢。我早就试过了。在这种情况下,在第一次刷卡时,mbIsFirstCall将设置为NO。我想这是一次检查,变量是否随时变为YES?否则,将满足每次else声明的要求。如果我错了,请纠正我。@I如果你对答案满意,记得投赞成票或接受它。:)很高兴你成功了!