【问题标题】:UICollectionView - Connect to custom cellUICollectionView - 连接到自定义单元格
【发布时间】:2014-04-22 15:33:13
【问题描述】:

我正在尝试构建一个 uiCollectionView,其中每个数据部分水平页面 - 该应用程序适用于私人教练,每个部分都是一个具有许多动态属性(速度、公里、时间等)的练习。

我已经为属性创建了一个自定义单元格,我只是处于尝试将自定义单元格应用于查看视图、填充标题和添加滚动的阶段 - 这是我的代码 -

- (void)loadView

{
//self.prefs = [NSUserDefaults standardUserDefaults];

//self.collectionView.backgroundView = [[UIImageView alloc] initWithImage:
                             //    [UIImage imageNamed:[self.prefs stringForKey:@"BGImageBlah"]]];




self.view = [[UIView alloc]
             initWithFrame:[[UIScreen mainScreen] bounds]
             ];

// Create a flow layout for the collection view that scrolls
// horizontally and has no space between items
UICollectionViewFlowLayout *flowLayout = [
                                          [UICollectionViewFlowLayout alloc] init
                                          ];
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
flowLayout.minimumLineSpacing = 5;
flowLayout.minimumInteritemSpacing = 0;

// Set up the collection view with no scrollbars, paging enabled
// and the delegate and data source set to this view controller
self.collectionView = [[UICollectionView alloc]
                       initWithFrame:self.view.frame
                       collectionViewLayout:flowLayout
                       ];
self.collectionView.showsHorizontalScrollIndicator = NO;
self.collectionView.pagingEnabled = YES;
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.view addSubview:self.collectionView];




}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.


}

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

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




- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
              cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // Dequeue a prototype cell and set the label to indicate the page
    WOColView *cell = [collectionView
                    dequeueReusableCellWithReuseIdentifier:@"CellWO"
                    forIndexPath:indexPath
                    ];
    cell.titleLbl.text = @"yo";

    // Switch colors to see view swipes... DELETE
    CGFloat hue = (CGFloat)indexPath.row / 9;
    cell.backgroundColor = [UIColor
                        colorWithHue:hue saturation:1.0f brightness:0.5f alpha:1.0f
                        ];
    cell.titleLbl.textColor = [UIColor whiteColor];
    cell.titleLbl.tintColor = [UIColor whiteColor];

    return cell;
}


- (CGSize)collectionView:(UICollectionView *)collectionView
              layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return collectionView.bounds.size;
}

这是我的故事板和连接的屏幕截图 -

这可以编译,但我得到的只是一个空的全屏颜色 - 它确实滚动 - 但在我的自定义单元格中没有任何其他证据!?我哪里错了。

【问题讨论】:

  • 需要检查的几件事:1) 您是否在cellForRowAtIndexPath 中设置了断点以验证您获得了一个单元格并且连接是否符合预期? 2) 单元格是否与集合视图在同一个故事板中定义(将集合视图单元格拖到界面构建器中的集合视图并从那里开始) 3) 您是否验证了集合视图的大小是否符合预期?跨度>
  • 谢谢 david - 1 - 它确实包含我的自定义单元格的一个版本 - 2 - 是的和 3 - 集合大小为 9 - 非常令人沮丧!不过感谢您的帮助。

标签: ios objective-c uicollectionview


【解决方案1】:

看起来您在情节提要中有一个包含自定义单元格的集合视图。如果确实如此,那么您不应该调用 loadView,在其中创建另一个集合视图 - 这是与您在情节提要中创建的集合视图不同的集合视图,并且其中不会包含您的单元格。尝试注释掉整个方法(您可以将任何您需要的语句放在 viewDidLoad 中 - 还要确保您有一个 IBOutlet 到集合视图,除非您使用的 UICollectionViewController 已经有了它)。

【讨论】:

    猜你喜欢
    • 2017-05-07
    • 1970-01-01
    • 1970-01-01
    • 2018-02-19
    • 1970-01-01
    • 2018-08-29
    • 2016-10-29
    • 1970-01-01
    相关资源
    最近更新 更多