ios - 在单击左右按钮时将 CollectionView 的单元格向左和向右滚动
<p><p>其实我有像这样的 <code>UICollectionView</code> 可见单元格</p>
<pre><code>- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
for (UICollectionViewCell *cell in ) {
NSIndexPath *indexPath = ;
lastIndex = - 1];
if (lastIndex ==9) {
;
}
if (lastIndex==0) {
;
}
if (lastIndex>3) {
;
}
if (lastIndex<6) {
;
}
}
}
</code></pre>
<p>现在通过使用该索引路径,我想左右滚动,但我很困惑在 newIndexPath 中写什么</p>
<pre><code>-(IBAction)RightButton:(id)sender
{
if (lastIndex <4 && lastIndex >9)
{
;
}
}
</code></pre>
<p>同样适用于左键</p>
<p>请帮忙
\</p>
<p>答案:</p>
<pre><code>NSUInteger lastIndex;
NSIndexPath *indexPathOf;
NSMutableArray *indexContainer;
NSIntegerIndexVisibleCell,IndexVisible;
NSArray *labelcontent;
NSArray *reversedArray;
-(void)awakeFromNib
{
_obj_CollectionView1.delegate = self;
_obj_CollectionView1.dataSource = self;
indexContainer = [initWithCapacity:15];
labelcontent = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10"];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
InnerCollectionCell1 *innerCell= ;
innerCell.innerViewCell1.layer.borderColor = .CGColor;
innerCell.innerViewCell1.layer.borderWidth = 1;
innerCell.innerLabelCell1.text = ;
return innerCell;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return ;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat Width = _obj_CollectionView1.frame.size.width-20;
float cellWidth =Width / 3.0;
CGSize size = CGSizeMake(cellWidth,_obj_CollectionView1.frame.size.height );
return size;
}
-(IBAction)RightButton:(id)sender
{
NSMutableArray *pat = [[ _obj_CollectionView1 indexPathsForVisibleItems] mutableCopy];
[pat sortUsingComparator:^NSComparisonResult(id obj1, id obj2)
{
NSInteger r1 = ;
NSInteger r2 = ;
if (r1 > r2) {
return (NSComparisonResult)NSOrderedDescending;
}
if (r1 < r2) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}];
NSIndexPath *pathh = item ]+1 inSection:0];
;
}
-(IBAction)LeftButton:(id)sender
{
NSMutableArray *pat = [[ _obj_CollectionView1 indexPathsForVisibleItems] mutableCopy];
[pat sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSInteger r1 = ;
NSInteger r2 = ;
if (r1 > r2) {
return (NSComparisonResult)NSOrderedDescending;
}
if (r1 < r2) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}];
NSIndexPath *pathh = item ]-1 inSection:0];
;
}
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
IndexVisible = - 1];
if (IndexVisible==labelcontent.count-1)
{
;
}
if (IndexVisible<-3)
{
;
}
if (IndexVisible >2) {
;
}
if (IndexVisible<1) {
;
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
for (UICollectionViewCell *cell in ) {
indexPathOf = ;
lastIndex = - 1];
if (lastIndex==labelcontent.count-1)
{
;
}
if (lastIndex<-3)
{
;
}
if (lastIndex >2) {
;
}
if (lastIndex<1) {
;
}
}}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>你的意思是说,一次最多应该有 3 个项目可见,对吧? </p>
<p>如果是这样,请创建 UICollectionViewFlowLayout 属性,以便最多 3 个可见</p>
<pre><code>- (void)calculateCollectionLayout
{
;
UICollectionViewFlowLayout *layout = ((UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout);
;
;
CGFloat cellWidth = ((CGRectGetWidth(self.view.frame) - (kNumberOfColumns + 1) * kPadding) - (2 * kMargin)) / kNumberOfItemsToBeVisible ;
;
;
}
</code></pre>
<p>在上面的代码中 <strong>kNumberOfItemsToBeVisible</strong> 将为 3</p>
<p>并根据项目总数显示/隐藏左右按钮跟踪currentLoadedItems</p>
<p>例如 currentPage 将是 3 并且 left 最初将被隐藏(如果它是 <= 3 隐藏左右按钮)。单击 Right 时,将其增加 3。并检查它是否 >= totalItems 。如果是,则隐藏右键。如果 currentpage 是 1 再次隐藏左键</p></p>
<p style="font-size: 20px;">关于ios - 在单击左右按钮时将 CollectionView 的单元格向左和向右滚动,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/37804642/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/37804642/
</a>
</p>
页:
[1]