未调用 Collection View 控制器中的自定义单元格
Custome cell in UICollectionView contoller not getting called
我像这样子class我的 UICollectionViewController 单元格:
-(CategoryScreenCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"cellForItemAtIndexPath load");
CategoryScreenCell *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
单元格标识符设置为 "MyCell",自定义单元格 class 设置为
"CategoryScreenCell"。 "CategoryScreenCell" Class:
@implementation CategoryScreenCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
NSLog(@"CategoryScreenCell before init");
if (self) {
NSLog(@"CategoryScreenCell inside init");
// Initialization code
_imageView = [[UIImageView alloc] initWithFrame:self.contentView.bounds];
[self.contentView addSubview:_imageView];
}
return self;
}
我正在使用故事板以及我的 UICollevtionViewController 的委托和来源
设置为拥有class.
这是将数据加载到我的 UICollectionView 的方法:
parser.completionBlock = ^(void) {
if (weakParser.appRecordList) {
dispatch_async(dispatch_get_main_queue(), ^{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
bundle: nil];
MyCollectionViewController *svc =[mainStoryboard instantiateViewControllerWithIdentifier:@"MainCategory"];
svc.entries = weakParser.appRecordList;
NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
NSIndexPath *indexPath;
for (int i = 0; i < [weakParser.appRecordList count]; i++) {
indexPath = [NSIndexPath indexPathForItem:i inSection:0];
[indexPaths addObject:indexPath];
}
[svc.collectionView reloadItemsAtIndexPaths:indexPaths];
});
}
self.queue = nil;
};
我正在为我的集合视图 (RFQuiltLayout) 使用客户布局。
我在我的故事板中将此布局设置为 collectionviewcontroller,因此它得到
带负载启动。
这是我的 MyCollectionViewController 代码:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.automaticallyAdjustsScrollViewInsets = NO;
NSLog(@"inside ");
NSLog(@"self.entries : %@ ",self.entries);
UIButton *button_right = [UIButton buttonWithType:UIButtonTypeCustom];
[button_right setImage:[UIImage imageNamed:@"setting_btn.png"] forState:UIControlStateNormal];
[button_right setFrame:CGRectMake(0, 0, 30, 30)];
SWRevealViewController *revealViewController = self.revealViewController;
if ( revealViewController )
{
[button_right addTarget:self.revealViewController action:@selector(revealToggle:) forControlEvents:UIControlEventTouchUpInside];
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
}
}
为了测试,我将 nslog 放在大多数地方(blockSizeForItemAtIndexPath,
insetsForItemAtIndexPath , numberOfItemsInSection) 它们都 returns 值
这是日志文件:
2015-03-07 11:52:46.001 News[9464:919944] numberOfSectionsInCollectionView :1
2015-03-07 11:52:46.001 News[9464:919944] numberOfItemsInSection : 10
2015-03-07 11:52:46.002 News[9464:919944] blockSizeForItemAtIndexPath
2015-03-07 11:52:46.005 News[9464:919944] insetsForItemAtIndexPath
2015-03-07 11:52:46.005 News[9464:919944] blockSizeForItemAtIndexPath
2015-03-07 11:52:46.006 News[9464:919944] insetsForItemAtIndexPath
2015-03-07 11:52:46.006 News[9464:919944] blockSizeForItemAtIndexPath
2015-03-07 11:52:46.006 News[9464:919944] insetsForItemAtIndexPath
2015-03-07 11:52:46.009 News[9464:919944] blockSizeForItemAtIndexPath
2015-03-07 11:52:46.009 News[9464:919944] insetsForItemAtIndexPath
2015-03-07 11:52:46.009 News[9464:919944] blockSizeForItemAtIndexPath
2015-03-07 11:52:46.010 News[9464:919944] insetsForItemAtIndexPath
2015-03-07 11:52:46.010 News[9464:919944] blockSizeForItemAtIndexPath
2015-03-07 11:52:46.010 News[9464:919944] numberOfSectionsInCollectionView load
如日志所示 "CategoryScreenCell" 未被调用。
这是我的整个 cellForItemAtIndexPath 方法:
-(CategoryScreenCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"cellForItemAtIndexPath load");
CategoryScreenCell *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
UILabel *label = (UILabel *)[myCell.contentView viewWithTag:10];
UILabel *catlbl = (UILabel *)[myCell.contentView viewWithTag:11];
UILabel *bglbl = (UILabel *)[myCell.contentView viewWithTag:12];
NSUInteger nodeCount = [self.entries count];
if (nodeCount == 0)
{
myCell.imageView.image = [UIImage imageNamed:@"logo.png"];
myCell.imageView.hidden = YES;
label.hidden = YES;
catlbl.hidden = YES;
}
if (nodeCount > 0)
{
catlbl.hidden=YES;
AppRecord *appRecord = [self.entries objectAtIndex:indexPath.item];
if(appRecord.title != NULL)
{
label.font = [UIFont fontWithName:@"B Yekan" size:12];
[label setText:appRecord.title];
NSLog(@"appRecord.title %@",appRecord.title);
[bglbl setText:@""];
[bglbl setBackgroundColor:[UIColor clearColor]];
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = bglbl.bounds;
gradientLayer.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:176.0f green:176.0f blue:176.0f alpha:0]CGColor], (id)[[UIColor colorWithRed:0 green:0 blue:0 alpha:1]CGColor], nil];
[bglbl.layer insertSublayer:gradientLayer atIndex:0];
}
NSString *id_tring= [NSString stringWithFormat:@"%@",appRecord.ids];
[catlbl setText:id_tring];
if (![appRecord.thumb_url isEqualToString:@""])
{
__block UIActivityIndicatorView *activityIndicator;
__weak UIImageView *weakImageView = myCell.imageView;
[myCell.imageView sd_setImageWithURL:[NSURL URLWithString:appRecord.thumb_url]
placeholderImage:nil
options:SDWebImageProgressiveDownload
progress:^(NSInteger receivedSize, NSInteger expectedSize) {
if (!activityIndicator) {
[weakImageView addSubview:activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]];
activityIndicator.center = weakImageView.center;
[activityIndicator startAnimating];
}
}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
[activityIndicator removeFromSuperview];
activityIndicator = nil;
}];
}
}
myCell.clipsToBounds = YES;
myCell.layer.masksToBounds = YES;
myCell.layer.cornerRadius = 6;
return myCell;
}
我该如何解决这个问题?
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self commonMethod ];
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
self=[super initWithCoder:aDecoder];
if (self) {
[self commonMethod ];
}
return self;
}
-(void)commonMethod
{
NSLog(@"CategoryScreenCell before init");
if (self) {
NSLog(@"CategoryScreenCell inside init");
// Initialization code
_imageView = [[UIImageView alloc] initWithFrame:self.contentView.bounds];
[self.contentView addSubview:_imageView];
_imageView.tag=8;
_label.tag=9;
_catlbl.tag=10;
//SO ON..
}
}
//假设你在 'commonMethod'
中给 imageView 的标签编号为 8
-(CategoryScreenCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"cellForItemAtIndexPath load");
CategoryScreenCell *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
UIImageView *imageView = (UIImageView *)[cell viewWithTag:8];
[imageView setImage : @"logo.png"];
[imageview setNeedsLayout];
//comment out the rest of code and try this and check whether image is seen in the cells
}
同时将 Storyboard 中 UICollectionViewCell 的 class 设置为 'yourCustomCollectionViewCell' class
您需要将自定义单元格 class 注册到您的集合视图
[_collectionView registerClass:[CategoryScreenCell class] forCellWithReuseIdentifier:@"MyCell"];
注册 class 后,只有您可以获得它的实例和内存
我像这样子class我的 UICollectionViewController 单元格:
-(CategoryScreenCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"cellForItemAtIndexPath load");
CategoryScreenCell *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
单元格标识符设置为 "MyCell",自定义单元格 class 设置为
"CategoryScreenCell"。 "CategoryScreenCell" Class:
@implementation CategoryScreenCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
NSLog(@"CategoryScreenCell before init");
if (self) {
NSLog(@"CategoryScreenCell inside init");
// Initialization code
_imageView = [[UIImageView alloc] initWithFrame:self.contentView.bounds];
[self.contentView addSubview:_imageView];
}
return self;
}
我正在使用故事板以及我的 UICollevtionViewController 的委托和来源
设置为拥有class.
这是将数据加载到我的 UICollectionView 的方法:
parser.completionBlock = ^(void) {
if (weakParser.appRecordList) {
dispatch_async(dispatch_get_main_queue(), ^{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
bundle: nil];
MyCollectionViewController *svc =[mainStoryboard instantiateViewControllerWithIdentifier:@"MainCategory"];
svc.entries = weakParser.appRecordList;
NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
NSIndexPath *indexPath;
for (int i = 0; i < [weakParser.appRecordList count]; i++) {
indexPath = [NSIndexPath indexPathForItem:i inSection:0];
[indexPaths addObject:indexPath];
}
[svc.collectionView reloadItemsAtIndexPaths:indexPaths];
});
}
self.queue = nil;
};
我正在为我的集合视图 (RFQuiltLayout) 使用客户布局。
我在我的故事板中将此布局设置为 collectionviewcontroller,因此它得到
带负载启动。
这是我的 MyCollectionViewController 代码:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.automaticallyAdjustsScrollViewInsets = NO;
NSLog(@"inside ");
NSLog(@"self.entries : %@ ",self.entries);
UIButton *button_right = [UIButton buttonWithType:UIButtonTypeCustom];
[button_right setImage:[UIImage imageNamed:@"setting_btn.png"] forState:UIControlStateNormal];
[button_right setFrame:CGRectMake(0, 0, 30, 30)];
SWRevealViewController *revealViewController = self.revealViewController;
if ( revealViewController )
{
[button_right addTarget:self.revealViewController action:@selector(revealToggle:) forControlEvents:UIControlEventTouchUpInside];
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
}
}
为了测试,我将 nslog 放在大多数地方(blockSizeForItemAtIndexPath, insetsForItemAtIndexPath , numberOfItemsInSection) 它们都 returns 值
这是日志文件:
2015-03-07 11:52:46.001 News[9464:919944] numberOfSectionsInCollectionView :1
2015-03-07 11:52:46.001 News[9464:919944] numberOfItemsInSection : 10
2015-03-07 11:52:46.002 News[9464:919944] blockSizeForItemAtIndexPath
2015-03-07 11:52:46.005 News[9464:919944] insetsForItemAtIndexPath
2015-03-07 11:52:46.005 News[9464:919944] blockSizeForItemAtIndexPath
2015-03-07 11:52:46.006 News[9464:919944] insetsForItemAtIndexPath
2015-03-07 11:52:46.006 News[9464:919944] blockSizeForItemAtIndexPath
2015-03-07 11:52:46.006 News[9464:919944] insetsForItemAtIndexPath
2015-03-07 11:52:46.009 News[9464:919944] blockSizeForItemAtIndexPath
2015-03-07 11:52:46.009 News[9464:919944] insetsForItemAtIndexPath
2015-03-07 11:52:46.009 News[9464:919944] blockSizeForItemAtIndexPath
2015-03-07 11:52:46.010 News[9464:919944] insetsForItemAtIndexPath
2015-03-07 11:52:46.010 News[9464:919944] blockSizeForItemAtIndexPath
2015-03-07 11:52:46.010 News[9464:919944] numberOfSectionsInCollectionView load
如日志所示 "CategoryScreenCell" 未被调用。
这是我的整个 cellForItemAtIndexPath 方法:
-(CategoryScreenCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"cellForItemAtIndexPath load");
CategoryScreenCell *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
UILabel *label = (UILabel *)[myCell.contentView viewWithTag:10];
UILabel *catlbl = (UILabel *)[myCell.contentView viewWithTag:11];
UILabel *bglbl = (UILabel *)[myCell.contentView viewWithTag:12];
NSUInteger nodeCount = [self.entries count];
if (nodeCount == 0)
{
myCell.imageView.image = [UIImage imageNamed:@"logo.png"];
myCell.imageView.hidden = YES;
label.hidden = YES;
catlbl.hidden = YES;
}
if (nodeCount > 0)
{
catlbl.hidden=YES;
AppRecord *appRecord = [self.entries objectAtIndex:indexPath.item];
if(appRecord.title != NULL)
{
label.font = [UIFont fontWithName:@"B Yekan" size:12];
[label setText:appRecord.title];
NSLog(@"appRecord.title %@",appRecord.title);
[bglbl setText:@""];
[bglbl setBackgroundColor:[UIColor clearColor]];
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = bglbl.bounds;
gradientLayer.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:176.0f green:176.0f blue:176.0f alpha:0]CGColor], (id)[[UIColor colorWithRed:0 green:0 blue:0 alpha:1]CGColor], nil];
[bglbl.layer insertSublayer:gradientLayer atIndex:0];
}
NSString *id_tring= [NSString stringWithFormat:@"%@",appRecord.ids];
[catlbl setText:id_tring];
if (![appRecord.thumb_url isEqualToString:@""])
{
__block UIActivityIndicatorView *activityIndicator;
__weak UIImageView *weakImageView = myCell.imageView;
[myCell.imageView sd_setImageWithURL:[NSURL URLWithString:appRecord.thumb_url]
placeholderImage:nil
options:SDWebImageProgressiveDownload
progress:^(NSInteger receivedSize, NSInteger expectedSize) {
if (!activityIndicator) {
[weakImageView addSubview:activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]];
activityIndicator.center = weakImageView.center;
[activityIndicator startAnimating];
}
}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
[activityIndicator removeFromSuperview];
activityIndicator = nil;
}];
}
}
myCell.clipsToBounds = YES;
myCell.layer.masksToBounds = YES;
myCell.layer.cornerRadius = 6;
return myCell;
}
我该如何解决这个问题?
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self commonMethod ];
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
self=[super initWithCoder:aDecoder];
if (self) {
[self commonMethod ];
}
return self;
}
-(void)commonMethod
{
NSLog(@"CategoryScreenCell before init");
if (self) {
NSLog(@"CategoryScreenCell inside init");
// Initialization code
_imageView = [[UIImageView alloc] initWithFrame:self.contentView.bounds];
[self.contentView addSubview:_imageView];
_imageView.tag=8;
_label.tag=9;
_catlbl.tag=10;
//SO ON..
}
}
//假设你在 'commonMethod'
中给 imageView 的标签编号为 8 -(CategoryScreenCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"cellForItemAtIndexPath load");
CategoryScreenCell *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
UIImageView *imageView = (UIImageView *)[cell viewWithTag:8];
[imageView setImage : @"logo.png"];
[imageview setNeedsLayout];
//comment out the rest of code and try this and check whether image is seen in the cells
}
同时将 Storyboard 中 UICollectionViewCell 的 class 设置为 'yourCustomCollectionViewCell' class
您需要将自定义单元格 class 注册到您的集合视图
[_collectionView registerClass:[CategoryScreenCell class] forCellWithReuseIdentifier:@"MyCell"];
注册 class 后,只有您可以获得它的实例和内存