【问题标题】:how to change the flow items order in collection view如何在集合视图中更改流项目顺序
【发布时间】:2016-06-20 10:23:49
【问题描述】:

默认情况下,在具有流式布局和水平方向的 UICollectionView 上,单元格从上到下,从左到右排序。像这样:

1 3 5 7  9 11
2 4 6 8 10 12

在我的例子中,collection view 是分页的,并且它的设计使得特定数量的单元格适合每个页面。因此,更自然的排序是:

1 2 3    7  8  9
4 5 6 - 10 11 12

我需要有相同的垂直流动的项目顺序但水平方向

我怎样才能做到这一点? 提前谢谢

【问题讨论】:

  • 我认为您需要使用另一个自定义布局。我猜流布局是这样工作的。

标签: ios swift sorting uicollectionview uicollectionviewcell


【解决方案1】:

实现 UICollectionView 并创建自定义集合视图单元格,其中包含 6 个标签 ---

1 2 3
4 5 6

在集合视图中设置项目数:-

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return array.count;
}

设置部分数量:-

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

根据您的要求在索引路径设置布局大小

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath 
{
    // set size of collection view cell (width, height)
    return CGSizeMake((self.view.frame.size.width)-40, 200);
}

集合视图数据具有不同的索引路径。在 CustomCollectionViewCell 类中制作标签的 IBOutlet 并连接到情节提要:-

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCollectionViewCell* customCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"waitingRoomTipId" forIndexPath:indexPath];

    // set data as per the requirement 
    customCell.label1.text = @"Label1";
    customCell.label2.text = @"Label2";
    customCell.label3.text = @"Label3";
    customCell.label4.text = @"Label4";
    customCell.label5.text = @"Label5";
    customCell.label6.text = @"Label6";

    return customCell;
}

这就是在每个单元格中显示六个标签的方式。

希望此解决方案对您有所帮助.. 谢谢

【讨论】:

    【解决方案2】:

    使用 UICollectionView 的 moveItemAtIndexPath sourceIndexPath: 来做到这一点。

    它具有源和目标 Indexpath 值。只需将用户集合视图单元拖放到您需要的任何位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-02
      • 2010-10-23
      • 1970-01-01
      • 2016-07-07
      • 2021-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多