• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 如何在 2 个不同的 UICollectionViewFlowLayout 实例之间切换?

[复制链接]
菜鸟教程小白 发表于 2022-12-13 07:29:21 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

这几天一直在努力。我试图让应用程序的客户在购物时在两种显示类型之间切换。

现在我正在使用 2 个 UICollectionView 和他们自己的自定义 UICollectionViewCell 并且两者之间的切换工作正常,但我发现使用 2 个不同的UICollectionView 正在成为一种痛苦。我为主要 UICollectionView 实现的某些东西在替代 UICollectionView 中无法正常工作。

这就是改变显示的方式。我使用带有自定义方法的 UISegmentedControl:

enter image description here

- (void)displayTypeSegmentSelected
{
    _selectedDisplayTypeIndex = [_displayTypeControl selectedSegmentIndex];


    if (_selectedDisplayTypeIndex == 0) {
        NSLog(@"Single file item view selected");
        _fromCollectionView = _collectionView;
        _toCollectionView = _collectionView2;


    } else if (_selectedDisplayTypeIndex == 1) {
        NSLog(@"Grid style view selected");
        _fromCollectionView = _collectionView2;
        _toCollectionView = _collectionView;
    }

    [_fromCollectionView removeFromSuperview];

    [_toCollectionView setFrame:[_superView bounds]];
    [_superView addSubview:_toCollectionView];

}

我通过 stackoverflow 获得了两个不同的建议,关于更好的方法来做我想做的事情。

  1. 使用 2 个 UICollectionViewFlowLayout 并在它们之间切换,而不是 2 个 UICollectionView
  2. 根据选择的段返回适当的自定义 UICollectionViewCell

我觉得第一种方法会更好。我需要将界面构建器 UICollectionViewFlowLayout 设为 IBOutlet,因为连接的 UICollectionView 不是以编程方式创建的。然后我会以编程方式创建第二个 UICollectionViewFlowLayout

在我的 displayTypeSegmentSelected 方法中,我将加载两个 UICollectionViewFlowLayout 之一,具体取决于所选的段。

单个文件显示如下所示:

enter image description here

网格样式显示如下所示:

enter image description here

问题:

我认为我有正确的想法来解决我最初的问题以及如何去做。但是,我会从更有经验的开发人员那里提出意见和示例。 你会怎么做?你能给我一个清晰的例子吗?

亲切的问候



Best Answer-推荐答案


这就是我最后做的事情。

点击特定的分段控件后,我更改了布局并重新加载了单元格。

- (void)displayTypeSegmentSelected
{
    _selectedDisplayTypeIndex = [_displayTypeControl selectedSegmentIndex];

    if (_selectedDisplayTypeIndex == 0) {
        NSLog(@"Single file item view selected");
        [_collectionView setCollectionViewLayout:_flowLayout2 animated:YES];
        [_collectionView reloadItemsAtIndexPaths:[_collectionView indexPathsForVisibleItems]];


    } else if (_selectedDisplayTypeIndex == 1) {
        NSLog(@"Grid style view selected");
        [_collectionView setCollectionViewLayout:_flowLayout animated:YES];
        [_collectionView reloadItemsAtIndexPaths:[_collectionView indexPathsForVisibleItems]];
    }

}

在我的 cellForItemAtIndexPath 中,根据屏幕上显示的布局,我加载了具有正确设置的特定自定义单元格。

-(UICollectionViewCell *)collectionViewUICollectionView *)collectionView cellForItemAtIndexPathNSIndexPath *)indexPath objectPFObject *)object
{

        NSLog(@"collectionview 1 loaded");

        static NSString *CellIdentifier = @"Cell";
        VAGGarmentCell *cell = [_collectionView dequeueReusableCellWithReuseIdentifier: CellIdentifier forIndexPath:indexPath];
        static NSString *CellIdentifier2 = @"Cell2";
        VAGGarmentCell2 *cell2 = [_collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier2 forIndexPath:indexPath];


    if ([[_collectionView collectionViewLayout] isEqual: _flowLayout]) {
        [[cell activityIndicator] startAnimating];

        PFFile *userImageFile = [object valueForKey"image"];
        [[cell imageView] setFile: userImageFile];
        [[cell imageView] loadInBackground];

        [[cell activityIndicator] stopAnimating];

        [[cell title] setText:[object valueForKey"title"]];
        [[cell price] setText:[NSString stringWithFormat: @"£%@ GBP", [object valueForKey"price"]]];
        return cell;

    } else if ([[_collectionView collectionViewLayout] isEqual: _flowLayout2]) {
        [[cell2 activityIndicator] startAnimating];

        PFFile *userImageFile = [object valueForKey"image"];
        [[cell2 imageView] setFile: userImageFile];
        [[cell2 imageView] loadInBackground];

        [[cell2 activityIndicator] stopAnimating];

        [[cell2 title] setText:[object valueForKey"title"]];
        [[cell2 price] setText:[NSString stringWithFormat: @"£%@ GBP", [object valueForKey"price"]]];
        return cell2;
    }

     return 0;

    //_addToFavouritesButton = [cell addFavouriteButton];

    [_addToFavouritesButton addTarget:_thisController actionselector(addToFavouritesButtonTapped forControlEvents:UIControlEventTouchUpInside];
}

在我的 viewDidLoad 中,我在这里引用了用于单个项目显示的自定义单元格的 nib 文件。

// Grab cell nib file and give a reuse identifier
[_collectionView registerNib:[UINib nibWithNibName"VAGGarmentCell2" bundle:nil] forCellWithReuseIdentifier"Cell2"];

我遇到了崩溃,但它们很难复制,尽管每次我在不同的布局之间切换时,我的内存都会不断增加。我猜我需要删除一些旧代码。我明天整理一下,用仪器检查一下。不过这已经完成了,我可以继续前进。

我可能需要重构我重复自己的代码。

关于ios - 如何在 2 个不同的 UICollectionViewFlowLayout 实例之间切换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23246022/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap