我不知道如何判断我是否到达最后IndexPath
以及何时“倒回”并滚动到第一个IndexPath
这是一些设置:
- (void)viewWillLayoutSubviews;
{
[super viewWillLayoutSubviews];
UICollectionViewFlowLayout *flowLayout = (id)self.collectionView.collectionViewLayout;
if (UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
flowLayout.itemSize = CGSizeMake(1024.0f, 768.0f);
} else {
flowLayout.itemSize = CGSizeMake(1024.0f, 768.0f);
}
[flowLayout invalidateLayout]; //force the elements to get laid out again with the new size
visibleItems = [self.collectionView indexPathsForVisibleItems];
self.currentIndexPath = [visibleItems firstObject];
[self.collectionView.collectionViewLayout invalidateLayout];
}
这是我的按钮代码:
- (IBAction)addToUploadQueque:(id)sender {
NSLog(@"current: %@",self.currentIndexPath);
NSInteger section = [self numberOfSectionsInCollectionView:self.collectionView] - 1;
NSInteger item = [self collectionView:self.collectionView numberOfItemsInSection:section]-1;
NSIndexPath *lastIndexPath = [NSIndexPath indexPathForItem:item inSection:section];
NSIndexPath *firstIndexpath =[NSIndexPath indexPathForItem:0 inSection:0];
NSLog(@"current: %@",lastIndexPath);
if (self.currentIndexPath <= lastIndexPath) {
NSInteger newLast = [self.currentIndexPath indexAtPosition:self.currentIndexPath.length-1]+1;
self.currentIndexPath = [[self.currentIndexPath indexPathByRemovingLastIndex] indexPathByAddingIndex:newLast];
[self.collectionView scrollToItemAtIndexPath:self.currentIndexPath
atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
animated:YES];
}else{
self.currentIndexPath = [visibleItems firstObject];
[self.collectionView scrollToItemAtIndexPath:self.currentIndexPath
atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
animated:YES];
}
}
我正在创建一个按钮,遍历集合视图中的每个单元格,当它到达结尾时(而不是超出界限)滚动回第一个单元格。
谁能告诉我我做错了什么?
答案 0 :(得分:1)
从您的评论来看,它听起来并不像您想要在一次通过中枚举可见单元格,而是想要手动枚举它们。听起来你在下一个NSIndexPath
时遇到了问题。您的代码段的问题在于您正在递增row
/ item
(取决于您是否正在处理UITableView
或UICollectionView
),但在尝试使用增量数据源之前,不要考虑是否已经到达某个部分的末尾,更不用说数据源的结尾了。
您可以执行以下操作:
NSInteger item = self.currentIndexPath.item;
NSInteger section = self.currentIndexPath.section;
item++; // go to next item
if (item >= [self.collectionView numberOfItemsInSection:section]) { // if you reached end of section ...
item = 0; // ... go to the start of the next section
section++;
if (section >= [self.collectionView numberOfSections]) { // if you reached the end of the data source ...
// all done, so set section to zero to go back to beginning, e.g. // ... then you're done
section = 0;
}
}
self.currentIndexPath = [NSIndexPath indexPathForItem:item inSection:section]; // otherwise, this is your new NSIndexPath
顺便说一句,如果我们专注于您的代码段,另一个问题是您使用<=
运算符来比较两个索引路径。你不能这样做。您必须使用compare
的{{1}}方法。
但是NSIndexPath
语句对我来说是不必要的,因为在最后一个索引路径之后数据源中没有索引路径。如果您正在递增逻辑(上面)正确检测到数据源的结尾,则不需要此if
语句。
此代码中存在许多问题。但是我想知道是否可以采用更简单的方法,而不是通过所有这些细节。如果您只想为所有可见行执行上传,也许您可以做一些更简单的事情,例如:
if
注意,您的代码正在滚动到单元格(可能是在上传完成时)。我可能试图阻止你从这种方法,但只是更新单元格(例如设置你的for (NSIndexPath *indexPath in self.collectionView.indexPathsForVisibleItems) {
// if the cell has some visual indication to reflect upload has been initiated,
// do that here
// do your asynchronous upload here, where the completion block dispatches
// updates to the cell/collectionView (to reflect that the individual upload
// is done)
}
方法引用的一些标志,然后在该行完成时为每一行调用cellForItemAtIndexPath
。因为上传可以如果这些异步上传完成,你可能不希望UI滚动。