ios8 UITableView滚动向上滚动时跳回

标签 ios objective-c uitableview ios7 ios8

我的 UItableView 有一个奇怪的情况。

我正在从 Internet 加载图像并以异步方式在 uitableviewcell 中呈现它们。当我向下滚动时(即第 1 行到第 6 行),滚动会像我预期的那样平滑地向下滚动。

但是,然后我向上滚动(例如第 6 行到第 1 行)滚动会跳回。例如,如果我从第 6 行滚动到第 5 行,它会跳回到第 6 行。我第二次尝试向上滚动时,它让我向上滚动到第 4 行,但随后它又将我滚动回第 5 行或第 6 行,但通常只有 5 个。

我不明白的是为什么这只发生在一个方向上。

它似乎影响 ios 8 但不影响 ios 7。

因此,在 ios8 和 7 处理 uitableview 的方式上存在实现差异。

我该如何解决这个问题?


这里有一些代码可以给你一些上下文

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CVFullImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    CVComicRecord *comicRecord = self.comicRecords[indexPath.row];
    [cell.loaderGear stopAnimating];
    [cell.text setText:comicRecord.title];
    NSString *UUID = comicRecord.fullImagePageURL.absoluteString;
    [cell setUUID:UUID];
    //Check if image is in the cache
    UIImage *fullImage = [self.contentViewCache objectForKey:UUID];
    [cell setComicFullImage:fullImage];

    if (fullImage) {
        return cell;
    }

    [cell.loaderGear startAnimating];

    [self requestImageForIndexPath:indexPath];
    return cell;
}

- (void)requestImageForIndexPath:(NSIndexPath *)indexPath {
    CVComicRecord *comicRecord = self.comicRecords[indexPath.row];
    NSString *UUID = comicRecord.fullImagePageURL.absoluteString;

    if ([self.contentViewCache objectForKey:UUID]) {
        //if it is already cached, I do not need to make a request.
        return;
    }

    id fd = CVPendingOperations.sharedInstance.fullDownloadersInProgress[UUID];

    if (fd) {
        //if it is in the queue you do no need to make a request
        return;
    }

    comicRecord.failedFull = NO;
    CVFullImageDownloader *downloader = [[CVFullImageDownloader alloc] initWithComicRecord:comicRecord withUUID:UUID];
    [CVPendingOperations.sharedInstance.fullDownloaderOperationQueue addOperation:downloader];
    //when operation completes it will post a notification that will trigger an observer to call fullImageDidFinishDownloading
}

- (void)fullImageDidFinishDownloading:(NSNotification *)notification {
    CVComicRecord *comicRecord = notification.userInfo[@"comicRecord"];
    NSString *UUID = notification.userInfo[@"UUID"];
    UIImage *fullImage = notification.userInfo[@"fullImage"];

    comicRecord.failedFull = NO;

    [self.contentViewCache setObject:fullImage forKey:UUID];
    dispatch_async(dispatch_get_main_queue(), ^{
        for (NSIndexPath *indexPath in [self.tableView indexPathsForVisibleRows]) {
            CVFullImageTableViewCell *cell = (id)[self.tableView cellForRowAtIndexPath:indexPath];
            if (cell) {
                if ([cell.UUID isEqualToString:UUID]) {
                    [cell.loaderGear stopAnimating];
                    [cell setComicFullImage:fullImage];
                    [cell layoutIfNeeded];
                }
            }
        }
    });
}

#pragma mark - Scroll

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    static int oldOffset = 0;
    int newOffset = scrollView.contentOffset.y;

    int dy = newOffset- oldOffset;
    if (dy > 0) {
        [self hideNavigationbar:YES animationDuration:0.5];
    } else  if (dy < 0) {
        [self hideNavigationbar:NO animationDuration:0.5];
    }

    oldOffset = newOffset;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (decelerate == NO) {
        [self prioritizeVisisbleCells];
        //currentPage is a property that stores that last row that the user has seen
        [self setCurrentPage:[self currentlyViewedComicIndexPath].row];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self prioritizeVisisbleCells];
    //currentPage is a property that stores that last row that the user has seen
    [self setCurrentPage:[self currentlyViewedComicIndexPath].row];
}

- (void)prioritizeVisisbleCells {
    NSArray *ips = [self.tableView indexPathsForVisibleRows];
    NSArray *activeIndexPaths = [CVPendingOperations.sharedInstance.fullDownloadersInProgress allKeys];

    //add visible cells to queue first
    NSSet *visible = [NSSet setWithArray:ips];
    NSMutableSet *invisible = [NSMutableSet setWithArray:activeIndexPaths];
    [invisible minusSet:visible];

    for (NSIndexPath *ip in invisible) {
        NSOperation *op = CVPendingOperations.sharedInstance.fullDownloadersInProgress[ip];
        [op setQueuePriority:NSOperationQueuePriorityNormal];
    }

    for (NSIndexPath *ip in visible) {
        NSOperation *op = CVPendingOperations.sharedInstance.fullDownloadersInProgress[ip];
        [op setQueuePriority:NSOperationQueuePriorityHigh];
    }
}

最佳答案

我发现这篇文章中的答案非常有帮助:Table View Cells jump when selected on iOS 8

尝试在 UITableViewDelegate 协议(protocol)中实现 tableView:estimatedHeightForRowAtIndexPath:。它对我有用。

关于ios8 UITableView滚动向上滚动时跳回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26106207/

相关文章:

ios - 在表格 View 单元格中单击按钮时如何使用参数调用 View Controller

ios - AFNetworking 2.0 iOS 7 在 AFHTTPRequestOperation.h 文件中复制带有区域警告

ios - 如何将 UIDatePicker 日期秒值设置为 00?

swift - 具有破坏性风格的 UIContextualAction 似乎默认删除行

ios - UITableView cellForRowAtIndexPath问题

IOS 静态表格 View 单元无法本地化

ios - 如何在另一个 View 上方显示 PickerView

iphone - 您如何使用 Objective-C 让声音文件自动播放到 iOS 应用程序并循环播放?

javascript - 使用 Phonegap 下载文件时 iOS 应用程序崩溃

php - 如何使用PHP和mysql数据库在XCode中成功登录后重定向到新 View