菜鸟教程小白 发表于 2022-12-11 16:43:32

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&gt;3) {
            ;
      }
      if (lastIndex&lt;6) {
            ;
      }
    }
}
</code></pre>

<p>现在通过使用该索引路径,我想左右滚动,但我很困惑在 newIndexPath 中写什么</p>

<pre><code>-(IBAction)RightButton:(id)sender
{
   if (lastIndex &lt;4 &amp;&amp; lastIndex &gt;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 = @[@&#34;1&#34;,@&#34;2&#34;,@&#34;3&#34;,@&#34;4&#34;,@&#34;5&#34;,@&#34;6&#34;,@&#34;7&#34;,@&#34;8&#34;,@&#34;9&#34;,@&#34;10&#34;];


}




-(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 &gt; r2) {
            return (NSComparisonResult)NSOrderedDescending;
      }
      if (r1 &lt; 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 &gt; r2) {
            return (NSComparisonResult)NSOrderedDescending;
      }
      if (r1 &lt; 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&lt;-3)
    {
      ;
    }
    if (IndexVisible &gt;2) {
      ;
    }
    if (IndexVisible&lt;1) {
      ;
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    for (UICollectionViewCell *cell in ) {
      indexPathOf = ;
      lastIndex = - 1];

      if (lastIndex==labelcontent.count-1)
      {
            ;
      }
      if (lastIndex&lt;-3)
      {
            ;
      }
      if (lastIndex &gt;2) {
            ;
      }
      if (lastIndex&lt;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]
查看完整版本: ios - 在单击左右按钮时将 CollectionView 的单元格向左和向右滚动