让 CollectionView 委托正确加载

Getting a CollectionView delegate to load properly

(让我先把错误放在这里,这样我就不会忘记它:无法使类型的视图出队:带有标识符 Cell 的 UICollectionElementKindCell - 必须为标识符注册一个笔尖或 class或在故事板中连接原型单元)

我有一个正常的 UICollectionViewController,它加载得很好。但我还有另一个控制器 (UserViewController) 加载 CollectionView 委托 (UserPlaceViewController).

我对必须在委托中加载的内容以及必须在当前控制器中加载的内容感到非常困惑。但无论如何,这是代表原样:

#import "UserPlaceViewController.h"

@interface UserPlaceViewController ()

@end

@implementation UserPlaceViewController
@synthesize placeArray, placeTable;

- (void)viewDidLoad
{
    [super viewDidLoad];
//    placeTable.delegate = self;

    UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(145, 150);
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    placeTable = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    //self.collectionView.frame = CGRectMake(10, 120, self.view.bounds.size.width-20, self.view.bounds.size.height-50);
    placeTable.autoresizesSubviews = YES;
    placeTable.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    //placeTable.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    placeTable.scrollEnabled = YES;

    self.view = placeTable;
    NSLog(@"%lu", (unsigned long)[placeArray count]);
}

这就是全部内容。最后还有一些其他的东西......但大部分就是这样。

下面是 ViewController 的相关部分:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    UserPlaceViewController *collectionView = [[UserPlaceViewController alloc] initWithNibName:@"UserPlace" bundle:nil];
    UINib * placeCell = [UINib nibWithNibName:@"Shops" bundle:nil];
    [collectionView.placeTable registerNib:placeCell forCellWithReuseIdentifier:cellIdentifier];

- (void) fetchedData: (NSData *) responseData
{
UserPlaceViewController * uptvLike = [[UserPlaceViewController alloc] init];
    if (IS_IPHONE5) {
        uptvLike.view.frame = CGRectMake(0, 0, 320, 300);
    }
    else
    {
    uptvLike.view.frame = CGRectMake(0, 0, 320, 200);
    }
    uptvLike.placeTable.delegate = self;
    uptvLike.placeTable.dataSource = self;
    uptvLike.placeTable.layer.borderWidth = 1.0f;
    uptvLike.placeTable.layer.borderColor = [UIColor colorWithRed:5/255.0f green:96/255.0f blue:255/255.0f alpha:1.0f].CGColor;
    [uptvLike.placeTable reloadData];
    [self.scrollView addSubview:uptvLike.view];
}

最后:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    PlaceCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];


    Place * p = [placeArray objectAtIndex:indexPath.item];
    cell.placeName.text = p.PName;
    cell.placeImg.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:p.PImage]]];
    return cell;
}

就像我说的:我知道 UICollectionView 应该如何工作。我有一个 运行。我知道你必须在 viewDidLoad 中注册一个 nib 或 class(我已经注册了一个 Nib。)但是我对如何加载这个委托以及在哪里加载什么感到困惑。例如,我看到我正在初始化委托和视图控制器中的集合视图。我也对我应该在哪里加载笔尖感到困惑......(代表们让我头疼......)

那到底是什么...正在下雨。我想我会问。

好的。你想要一个 UICollectionView 但你还想在同一个屏幕上有很多常规 "View" 项目?有两点很重要:

1. 你不需要两个 ViewController。您只需要加载通常是您的 UICollectionViewController 作为常规 ViewController:

@interface UserViewController : UIViewController <UICollectionViewDataSource, UIScrollViewDelegate>

2. 完成后,您可以将集合视图加载到该视图的顶部:

UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(145, 150);
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout];
 [self.scrollView addSubview:self.collectionView];
    [self.view addSubview:HUD];

    UINib * placeCell = [UINib nibWithNibName:@"UserCell" bundle:nil];

    [self.collectionView registerNib:placeCell forCellWithReuseIdentifier:@"Cell"];

剩下的只是您的标准 CollectionView 内容。