gpt4 book ai didi

ios8 UITableView滚动向上滚动时跳回

转载 作者:IT王子 更新时间:2023-10-29 08:04:01 25 4
gpt4 key购买 nike

我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com