簡體   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