ios - 无法将委托(delegate)方法分配给 UIViews?

标签 ios objective-c uiview

我定义了一个简单的 CollectionView View ,

当我将委托(delegate)方法放在 ViewController 中时,单元格会正确显示。但是,当我将它移动到 UIView 时,没有调用任何委托(delegate)方法。

有人知道为什么吗?附上代码。

在ViewController中,viewDidLoad方法,

self.userGrid = [[UserGrid alloc] initWithFrame:self.view.frame];

UserGrid.h

#import <UIKit/UIKit.h>
#import "AvatarViewCell.h"
#import "GridViewLayout.h"

@interface UserGrid : UIView <UICollectionViewDelegate, UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@end

和UserGrid.m

#import "UserGrid.h"

@implementation UserGrid

- (id)initWithFrame:(CGRect)aRect
{
    UINib *cellNib = [UINib nibWithNibName:@"AvatarViewCell" bundle:nil];
    [self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"avatarCell"];

    if ((self = [super initWithFrame:aRect])) {
        self.collectionView.dataSource = self;
        self.collectionView.delegate   = self;

        [self.collectionView registerClass:[AvatarViewCell class] forCellWithReuseIdentifier:@"avatarCell"];
    }
    return self;
}

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

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"avatarCell";

    AvatarViewCell *avatarCell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    [avatarCell.avatarImageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"website_%ld.jpg", indexPath.row + 1]]];

    return avatarCell;
}

@end

** 编辑

我将 init 部分更改为(在 viewDidLoad 中),

self.userGrid = [[[NSBundle mainBundle] loadNibNamed:@"UserGrid" owner:nil options:nil] lastObject];
self.userGrid.frame = self.view.frame;

在viewDidAppear中添加,

- (void)viewDidAppear:(BOOL)animated {
    [self.userGrid.collectionView reloadData];
}

添加了 awakeFromNib 方法,但是没有调用委托(delegate)方法......嗯

- (void)awakeFromNib {
    UINib *cellNib = [UINib nibWithNibName:@"AvatarViewCell" bundle:nil];
    [self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"avatarCell"];

    NSLog(@"awake");
    self.collectionView.dataSource = self;
    self.collectionView.delegate   = self;

    [self.collectionView registerClass:[AvatarViewCell class] forCellWithReuseIdentifier:@"avatarCell"];
}

最佳答案

像这样初始化 UserGrid View

    self.userGrid=[[[NSBundle mainBundle] loadNibNamed:@"yourxibName" owner:nil options:nil] lastObject];
    self.userGrid.frame=self.view.frame;

你有一个 collectionview 的 IBOutlet 但没有将 View 附加到你的类所以集合不会被初始化

关于ios - 无法将委托(delegate)方法分配给 UIViews?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37088877/

相关文章:

ios - 从 iOS 开发者库中的框架访问系统信息

objective-c - 如何使用滑动手势从单元格 View 返回到 TableView

ios - 如何动画缩放和移动 UILabel,然后在完成后将其变换设置回身份并保留其框架?

ios - 带有 Storyboard的 Swift PNChart PNCircleChart

ios - 检测换行符以在 iOS 上的 UITextView 上自动输入换行文本

ios - 将强度参数添加到 GPUImageFalseColorFilter

java - 为什么 DesEncrypter 的 iOS 实现不会产生与 Java 相同的结果?

iphone - 我们可以使用相同的 Xib(包含 UIView)来支持横向和纵向吗

ios - 跨多个 subview 处理触摸事件

ios - 需要 Objective C 重复回调