Tôi đang phát triển một ứng dụng mà tôi có UICollectionView cuộn vô hạn của hình ảnh. Khi người dùng đến cuối trang, tôi truy vấn trang tiếp theo của hình ảnh, lấy các indexPath mới và thêm chúng vào UICollectionView với [collectionView insertItemsAtIndexPaths:newIndexPaths]
.iOS 7: insertItemsAtIndexPaths: cuộn UICollectionView
Tất cả điều này hoạt động hoàn hảo trong iOS 6. Trong iOS 7 nó hoạt động, nhưng nó cuộn người dùng trở lại lên trên cùng của bộ sưu tậpXem mỗi lần. Tôi đã thử sử dụng reloadData
thay vào đó và điều tương tự cũng xảy ra.
Bất kỳ ý tưởng nào về cách tôi có thể ngăn cuộn này?
UPDATE: Bao gồm mã của tôi
Tôi có một UICollectionView hiển thị hình thu nhỏ của hình ảnh trong một mô hình khảm (mỗi hình ảnh là một kích thước khác nhau, vì vậy tôi đang sử dụng RFQuiltLayout cho điều này tôi không nghĩ vậy. Điều này có liên quan đến vấn đề cuộn, vì mã nguồn dường như không liên quan đến việc di chuyển.
Tôi tải mỗi bộ 12 hình ảnh từ phần phụ trợ Parse của chúng tôi với mã sau đây (Nếu isRefresh
là đúng) ra khỏi mảng hình ảnh và tải lại trang đầu tiên):
- (void)loadImagesStartingAtNum:(int)num withRefresh:(BOOL)isRefresh {
NSLog(@"Starting query...");
[PFCloud callFunctionInBackground:@"homeFeed" withParameters:@{@"startNum": [NSNumber numberWithInt:num]} block:^(id objects, NSError *error) {
if(!error) {
//NSLog(@"Got back home feed objects: %@", objects);
NSMutableArray *newIndexPaths = [[NSMutableArray alloc] init];
if (isRefresh) {
[imagesArray removeAllObjects];
[imageURLsArray removeAllObjects];
page = 0;
for (PFObject *object in objects) {
[imagesArray addObject:object];
[imageURLsArray addObject:[XSUtilities urlForImageObject:object]];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:([imagesArray count] - 1) inSection:0];
NSLog(@"New row: %d", indexPath.row);
[newIndexPaths addObject:indexPath];
}
if ([newIndexPaths count] > 0) {
[feed reloadData];
[refreshControl endRefreshing];
page += 1;
}
} else {
for (PFObject *object in objects) {
[imagesArray addObject:object];
[imageURLsArray addObject:[XSUtilities urlForImageObject:object]];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:([imagesArray count] - 1) inSection:0];
NSLog(@"New row: %d", indexPath.row);
[newIndexPaths addObject:indexPath];
}
if ([newIndexPaths count] > 0) {
[UIView animateWithDuration:0.25 animations:^{loadingLabel.alpha = 0.0;}];
[feed insertItemsAtIndexPaths:newIndexPaths];
//[feed scrollToItemAtIndexPath:newIndexPaths[0] atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:NO];
NSLog(@"imagesArray count: %d", [imagesArray count]);
//[feed reloadData];
page += 1;
} else {
NSLog(@"Got back no objects");
}
}
}
isLoadingNextPage = NO;
}];
}
sau đó tôi phát hiện khi người dùng cuộn đến cuối trang và tải các tập tiếp theo với mã này:
- (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
CGPoint offset = aScrollView.contentOffset;
CGRect bounds = aScrollView.bounds;
CGSize size = aScrollView.contentSize;
UIEdgeInsets inset = aScrollView.contentInset;
float y = offset.y + bounds.size.height - inset.bottom;
float h = size.height;
float reload_distance = 480;
if(y > h - reload_distance) {
NSLog(@"Hit the load point");
if (!isLoadingNextPage) {
NSLog(@"Loading page %d", page);
isLoadingNextPage = YES;
[self loadImagesStartingAtNum:(page * 12) withRefresh:NO];
}
}
}
Tôi không thể tạo lại điều này. Bạn có thể xem chế độ xem cuộn cuộn vô hạn mà tôi [nhốt ở đây] (https://dl.dropboxusercontent.com/u/2183704/Infinite%20Scroll%20Collection%20View.zip). Mỗi lần bạn nhấn vào đáy nó sẽ thêm 200 ô nữa. Vì vậy, bạn có thể hiển thị mã của bạn? –