简体   繁体   English

UICollectionView-连接到自定义单元格

[英]UICollectionView - Connect to custom cell

I am trying to build a uiCollectionView which pages horizontally per data section - the App is for personal trainers and each section is an exercise which has a number of dynamic attributes (speed, km's, time etc). 我正在尝试构建一个uiCollectionView,每个数据部分的水平页面-该应用程序是针对私人教练的,每个部分都是一个练习,它具有许多动态属性(速度,公里,时间等)。

I have created a custom cell for the attributes and i'm simply at the stage of trying to apply the custom cell to view view, populating a title and adding the scrolling - here is my code - 我已经为属性创建了一个自定义单元格,而我只是在尝试将自定义单元格应用于视图,填充标题并添加滚动的阶段-这是我的代码-

- (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;
}

this is a screenshot of my storyboard and connections - 这是我的故事板和关联的屏幕截图-

在此处输入图片说明

This compiles ok but all i get is an empty full screen of color - which does scroll - but theres no evidence of anything else in my custom cell!? 这样可以编译,但是我得到的只是一个空的全屏颜色-可以滚动-但自定义单元中没有任何其他证据! Where am I going wrong. 我要去哪里错了。

It looks like you have a collection view with your custom cell in the storyboard. 看起来您在情节提要中具有自定义单元格的集合视图。 If that is indeed the case, then you shouldn't be calling loadView, where you create another collection view -- this is a different collection view from the one you created in the storyboard, and won't have your cell in it. 如果确实如此,那么您不应该调用loadView来在其中创建另一个集合视图-这是与在情节提要中创建的视图不同的集合视图,并且其中不会包含单元格。 Try commenting out that whole method (you can put any of those statements that you need in viewDidLoad instead -- also make sure you have an IBOutlet to the collection view unless you're using a UICollectionViewController which already has that). 尝试注释掉整个方法(您可以将所需的任何语句放入viewDidLoad中-还要确保对集合视图具有IBOutlet,除非您正在使用已经具有该属性的UICollectionViewController)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM