【问题标题】:Getting a CollectionView delegate to load properly让 CollectionView 委托正确加载
【发布时间】:2015-07-28 16:00:19
【问题描述】:

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

我有一个加载正常的常规 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 或类(我已经注册了一个 Nib。)但是我对如何准确加载这个委托以及在哪里加载什么感到困惑。例如,我看到我正在委托和视图控制器中初始化集合视图。我也对我应该在哪里加载笔尖感到困惑......(代表们让我头疼......)

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

【问题讨论】:

    标签: delegates xcode6 uicollectionview viewcontroller


    【解决方案1】:

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

    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 东西。

    【讨论】:

      猜你喜欢
      • 2022-07-05
      • 2013-11-26
      • 1970-01-01
      • 2016-07-05
      • 1970-01-01
      • 2010-11-16
      • 2013-12-20
      • 2012-11-01
      • 1970-01-01
      相关资源
      最近更新 更多