【问题标题】:iOS 7.1 Nested UICollectionView in a UIPopoveriOS 7.1 在 UIPopover 中嵌套 UICollectionView
【发布时间】:2014-07-29 19:11:27
【问题描述】:

我正在与一个小型开发团队合作开发一个适用于 iPad 的 iOS 项目。该项目需要一个 UICollectionView 嵌套在里面的 UIPopover。该系列最终将成为图标。所以最后,它将是一个包含动态选择图标网格的弹出框。不幸的是,我看到的 UICollectionViews 教程大多是单视图应用程序或使用故事板(我的小组正在使用 .xib 文件)。我能够显示弹出窗口,但我不太确定如何设置 UICollectionView 和网格的自定义单元格。我曾尝试在 popover 方法中创建集合视图,但效果不佳。我目前正在尝试创建一个自定义 UICollectionViewController 子类。弹出框显示 UICollectionView,但不显示自定义单元格。

这是我的弹出框方法:

- (IBAction)displayPopover{
    WMGAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    //image array is the array which will hold the dynamically chosen icons
    [self populateImageArray];    
    // Configure layout
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setItemSize:CGSizeMake(130, 130)];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    // create collectionview
    WMGCollectionViewController *gridController = [[WMGCollectionViewController alloc] initWithCollectionViewLayout:flowLayout];
    [gridController.collectionView registerNib:[UINib nibWithNibName:@"CVCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"cvCell"];    
    //if popover is already visible
    if ([self.selectorController isPopoverVisible]) {
        [self.selectorController dismissPopoverAnimated:YES];
    }
    //popover not visible
    else {

        self.selectorController = [[UIPopoverController alloc]initWithContentViewController:gridController];
        self.selectorController.delegate = self;
        [self.selectorController presentPopoverFromRect:_firstImage.frame inView:self.view permittedArrowDirections: UIPopoverArrowDirectionLeft | UIPopoverArrowDirectionDown animated:YES];
        self.selectorController.popoverContentSize = CGSizeMake(450, 500);
    }
}

我的自定义 UICollectionViewController 类文件如下所示:

#import <UIKit/UIKit.h>

@interface WMGCollectionViewController : UICollectionViewController

@property (nonatomic, strong) UICollectionView *grid;

@end

#import "WMGCollectionViewController.h"
#import "CVCell.h"

@interface WMGCollectionViewController ()

@end

@implementation WMGCollectionViewController

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section;
{
    return 32;
}

- (CVCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
    // we're going to use a custom UICollectionViewCell, which will hold an image and its label
    //
    // Setup cell identifier
    static NSString *cellIdentifier = @"cvCell";
    CVCell *cell = [cv dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

    // make the cell's title the actual NSIndexPath value
    cell.titleLabel.text = [NSString stringWithFormat:@"{%ld,%ld}", (long)indexPath.row, (long)indexPath.section];

    return cell;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
    {
        [super viewDidLoad];
        self.collectionView.backgroundColor = [UIColor colorWithWhite:0.25f alpha:1.0f];
    }

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

据我所知,我所有的网点都设置得很好。我在做一些明显错误的事情吗?任何帮助将不胜感激。

【问题讨论】:

  • UIViewController“控制器”的作用是什么。为什么不让 WMGCollectionViewController 成为弹出框的内容控制器呢?此外,您还有 WMGCollectionViewController *gridController = [WMGCollectionViewController alloc] 这一行。你永远不应该有一个没有 init 的 alloc。您也不应该为集合视图使用 IBOutlet,UICollectionViewController 已经有一个 collectionView 属性(因此您也不应该在 initWithNibName 中分配一个集合视图)。
  • @rdelmar UIViewController 是使用滚动视图的先前实现遗留下来的。感谢您的小费。我对 Objective C 还是很陌生。这些问题中的任何一个都会导致 UICollectionView 不显示吗?
  • @rdelmar 当我尝试将 WMGCollectionViewController 设置为内容控制器时,应用程序在 initWithContentViewController 行崩溃。
  • 你修复了只有 alloc 而没有 init 的行吗?那次崩溃有什么错误吗?是的,其中一些问题可能会导致集合视图不显示。
  • @rdelmar 我确实先修复了没有初始化的分配。在经历了几次崩溃后,我得到了显示的集合视图。我还不能让单元格显示出来。

标签: ios objective-c ipad uicollectionview uipopover


【解决方案1】:

我没有为我创建的自定义单元注册 Nib。

WMGCollectionViewController *gridController = [[WMGCollectionViewController alloc] initWithCollectionViewLayout:flowLayout];
[gridController.collectionView registerNib:[UINib nibWithNibName:@"CVCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"cvCell"];

一旦我将registerClass... 更改为registerNib...(如编辑后的问题所示),就会出现单元格。

【讨论】:

    猜你喜欢
    • 2021-12-16
    • 2021-02-10
    • 2021-12-10
    • 2012-11-25
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    • 2012-09-21
    相关资源
    最近更新 更多