iOS7 UICollectionViewCell not being reused when dequeuing cell - ios

the method
-(void)prepareForReuse
In my collection view cell is never called - leading me to suspect that the UICollectionView is not dequeuing cells properly. This is causing lagyness and memory issues.
I've set up my collectionView as follows:
static NSString *cellIdentifier = #"Mycell";
-(void)initMyView
{
[self.collectionView registerClass:[UrlLoadableCollectionViewCell class] forCellWithReuseIdentifier:cellIdentifier];
}
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UrlLoadableCollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:index];
if (cell.contentView.frame.size.width < 100) // tried removing this as well but didn't help
{
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
} else {
cell.layer.shouldRasterize = NO;
}
// prepare cell
}
EDIT
Additional Code
static NSString *cellIdentifier = #"Mycell";
#interface UIThumbnailGalleryView
#property (nonatomic,strong) UICollectionView *collectionView;
#end
#implementation UIThumbnailGalleryView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self initView:frame];
}
return self;
}
-(void)initView:(CGRect)frame
{
self.collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:[self getGalleryLayout]];
[self.collectionView registerClass:[UrlLoadableCollectionViewCell class] forCellWithReuseIdentifier:cellIdentifier];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[self addSubview:self.collectionView];
self.collectionView.backgroundColor = [UIColor blackColor];
[self.collectionView setShowsHorizontalScrollIndicator:NO];
[self.collectionView setShowsVerticalScrollIndicator:NO];
}
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UrlLoadableCollectionViewCell *cell = [self.dequeueReusableCellAtIndex:indexPath];
}
-(UrlLoadableCollectionViewCell *)dequeueReusableCellAtIndex:(NSIndexPath *)index
{
UrlLoadableCollectionViewCell *cell = (UrlLoadableCollectionViewCell *)[self.collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:index];
return cell;
}
-(UICollectionViewFlowLayout *)getGalleryLayout
{
UICollectionViewFlowLayout *galleryLayout = [[UICollectionViewFlowLayout alloc] init];
[galleryLayout setItemSize:CGSizeMake(77, 77)];
galleryLayout.minimumInteritemSpacing = 3.0;
galleryLayout.minimumLineSpacing = 3.0;
// iOS 6 - might need to uncomment
//[galleryLayout setSectionInset:UIEdgeInsetsMake(44,5, 44, 5)];
return galleryLayout;
}

Please post a screenshot of the xib/scene that contains your UICollectionViewCell showing it's inspector. Or, if you're cell is constructed entirely in code, post the relevant code that registers your class with the collection view. Usually when this occurs it's because of a typo in the Cell Identifier.

You don't seem to be calling initMyView anywhere.

Related

cannot register delegate and data source of UITableView inside UITableViewCell

it says
Assigning to 'id _Nullable' from incompatible
type 'ChildTableViewCell *__strong'
is there any mistake if i implement UITableView cell inside custom UITableViewCell ?
So first in MainViewController, Using MainView that have UITableView,
in the MainViewController, especially in ViewDidLoad, i register self.mainView.tableView.delegate = self; also self.mainView.dataSource = self;
and in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath i have this:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([self.filteredCategoriesAllListLocalDataArray count] > 0) {
static NSString *cellID = #"ChildTableViewCell";
ChildTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if(nil == cell) {
cell = [[ChildTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellID];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
CategoryModel *currentCategory = [self.filteredCategoriesAllListLocalDataArray objectAtIndex:indexPath.section];
ChildModel *currentChildData = [currentCategory.childModelArray objectAtIndex:indexPath.row];
NSLog(#"================== %#", currentChildData.nameString);
cell.childData = currentChildData;
[cell initializeChildTableView];
cell.clipsToBounds = YES;
return cell;
}
UITableViewCell *cell = [[UITableViewCell alloc] init];
return cell;
}
and then in ChildTableViewCell i have this:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, CGRectGetWidth([UIScreen
mainScreen].bounds), CGRectGetHeight(self.contentView.frame))style:UITableViewStyleGrouped];
self.tableView.showsVerticalScrollIndicator = NO;
self.tableView.showsHorizontalScrollIndicator = NO;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
self.tableView.backgroundColor = [UIColor whiteColor];
self.tableView.scrollEnabled = NO;
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.contentView addSubview:self.tableView];
}
return self;
}
but in step 4, got this error message:
Assigning to 'id _Nullable' from incompatible
type 'ChildTableViewCell *__strong'
im Using UITableView inside UItableViewCell cause want to make UITAbleView Tree Level with custom data, i've been search all reference for UITableView Multi level but it doesn't help at all.

add UICollectionView as subview of UICollectionReusableView reloadData not work, what I miss?

I'm try to add UICollectionView to my custom UICollectionReusableView in runtime, in main form I have a UICollectionView(named mainView) to show first-level menus to user, when user did select cell in mainView it will expand second-level menus in foot section(and call FolderContentView::loadSubMenus method),
following is my wont work code, please help.
.h file
#interface FolderContentView : UICollectionReusableView<UICollectionViewDataSource,UICollectionViewDelegate>
-(void) loadSubMenus:(NSArray<EnabledModule*>*) subModules;
#end
.m file
#import "FolderContentView.h"
#implementation FolderContentView {
UICollectionView *_collectionView;
NSArray* _subMenus;
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.itemSize = CGSizeMake(100, 100);
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
_collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:flowLayout];
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"cell"];
_collectionView.delegate = self;
_collectionView.dataSource = self;
}
[_collectionView reloadData];
return self;
}
-(void) loadSubMenus:(NSArray<EnabledModule*>*) subModules {
if(subModules != nil) {
_subMenus = [subModules copy];
} else {
_subMenus = nil;
}
[_collectionView reloadData];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return _subMenus!=nil?_subMenus.count:0;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView
dequeueReusableCellWithReuseIdentifier:#"cell"
forIndexPath:indexPath];
if(!cell){
cell=[[UICollectionViewCell alloc]init];
}
EnabledModule *model = _subMenus[indexPath.row];
cell.hidden = !model.enabled;
UILabel *lblText = [[UILabel alloc] init];
lblText.text = model.moduleName;
[cell.contentView addSubview:lblText];
return cell;
}
#end
try reloadData from main thread, it's wont work.
set bp to numberOfItemsInSection numberOfItemsInSection, not hint.
try to set delegate and dataSource to my mainView's controller, not work too.
What's 'reloadData not work' actually means? Did the delegate methods run correctly or nothing happen?
1.Maybe you should check the codes 'add UICollectionView to my custom UICollectionReusableView in runtime'.
2.Maybe you forgot to do:
[self addSubview:_collectionView];
3.The 'cell' will not be nil in this code:
UICollectionViewCell *cell = [collectionView
dequeueReusableCellWithReuseIdentifier:#"cell"
forIndexPath:indexPath];
if(!cell){
cell=[[UICollectionViewCell alloc]init];
}
4.Don't do this:
[cell.contentView addSubview:lblText];
Use subClass of UICollectionViewCell instead.

Set Value via Custom Table Cell

In the following custom table cell (StepperProgressTableViewCell), I have hardcoded currentSection to 2 for testing, but when it hits StepperProgressTableViewCell awakeFromNib method, it shows 0.
TableViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"StepperProgressCell";
StepperProgressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (!cell) {
cell = [[StepperProgressTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.stepperTableCellDelegate = self;
cell.currentSection = 2;
return cell;
}
StepperProgressTableViewCell.m
#implementation StepperProgressTableViewCell
#synthesize stepperView, currentSection;
- (void)awakeFromNib {
[super awakeFromNib];
[self setUpViews];
// it always shows 0
NSLog(#"%lu", currentSection);
}
- (void)setUpViews {
self.stepperView = [[AYStepperView alloc]initWithFrame:CGRectMake(0, 0 , [[UIScreen mainScreen] bounds].size.width, kFormStepperViewHeight) titles:#[NSLocalizedString(#"Processing", nil), NSLocalizedString(#"Ready", nil), NSLocalizedString(#"Delivered", nil)]];
self.stepperView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
self.stepperView.userInteractionEnabled = YES;
self.stepperView.currentSection = currentSection
[self addSubview:self.stepperView];
}
AYStepperView.m
- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)titles {
self = [super initWithFrame:frame];
if (!self) {
return nil;
}
// ultimate goal is to set AYStepperView's currentSection
// the following also prints 0
NSLog(#"%lu", _currentSection);
return self;
}
You need add a method to change this property and in the implementation pass the value to your stepperView
StepperProgressTableViewCell.h
add a method like
-(void)setupCurrentSection:(int)newCurrentSection;
StepperProgressTableViewCell.m
-(void)setupCurrentSection:(int)newCurrentSection{
self.currentSection = newCurrentSection
self.stepperView.currentSection = newCurrentSection
}
in your cellForRow then add this line
[cell setupCurrentSection:2];
Hope this helps

Small UIImage view in custom UICollectionViewCell

I have UICollectionViewCell
#import "DBPhotoCollectionViewCell.h"
#define IMAGEVIEW_BORDER_LENGTH 5
#implementation DBPhotoCollectionViewCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setup];
}
return self;
}
/* Need to implement the initWithCoder method since this class will be created from the storyboard */
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self){
[self setup];
}
return self;
}
/* Create the UIImageView and add it to the cell's contentView in code. */
-(void)setup
{
self.imageView = [[UIImageView alloc] initWithFrame:CGRectInset(self.bounds, IMAGEVIEW_BORDER_LENGTH, IMAGEVIEW_BORDER_LENGTH)];
[self.contentView addSubview:self.imageView];
}
#end
And call this Cell from CollectionViewController class
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Photo Cell";
DBPhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
cell.imageView.image = [UIImage imageNamed:#"Astronaut.jpg"];
// Configure the cell
return cell;
}
But I have one problem when I start my application my picture is showed with small size. What I missed?
http://screencast.com/t/ia8tE05P6yc
http://screencast.com/t/33N4AhT7
cell.imageView, imageView object is not same as which you have added on cell of Interface Builder. UICollectionViewCell has default size UIImageView which are accessing, you can access your customView imageView in cellForIndexPath method of collectionView by linking imageView of subClass DBPhotoCollectionViewCell in interface builder you don't need to add [self.contentView addSubview:self.imageView]; in setup method.
Second approach would be assign tag value to imageView when you adding to cell contentView
/* Create the UIImageView and add it to the cell's contentView in code. */
-(void)setup
{
self.imageView = [[UIImageView alloc] initWithFrame:CGRectInset(self.bounds, IMAGEVIEW_BORDER_LENGTH, IMAGEVIEW_BORDER_LENGTH)];
self.imageView.tag=1;
[self.contentView addSubview:self.imageView];
}
//And access imagView in cellForIndexPath
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Photo Cell";
DBPhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
UIImageView *imageView=[cell.contentView viewWithTag:1];
imageView.image = [UIImage imageNamed:#"Astronaut.jpg"];
// Configure the cell
return cell;
}

View description in myUICollectionView didn't work

I'm a beginner to learn ios. It's nice to be here! I have some problem using the UICollectionView.
I've add this in myUICollectionCell.m and linked the item to storyBoard
#import "QuartzCore/QuartzCore.h"
#implementation myCollectionViewCell
#synthesize myImageView
-(instancetype)initWithFrame:(CGRect)frameRect {
self = [super initWithFrame:frameRect];
if (self) {
myImageView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 200, 60, 60)];
myImageView.layer.cornerRadius = view2.frame.size.width/2;
myImageView.layer.masksToBounds = YES;
[self.contentView addSubview:myImageView];
}
return self;
}
#end
ViewController.m
#import "myCollectionViewCell.h"
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString * CellIdentifier = #"cell";
myCollectionViewCell *cell = (myCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.myImageview.image = [UIImage imageNamed:#"2.jpg"];
return cell;
}
myImageView still didn't change to circle as I described.
It can only work when I put the description into collectionView:cellForItemAtIndexPath:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString * CellIdentifier = #"cell";
myCollectionViewCell *cell = (myCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.myImageview.image = [UIImage imageNamed:#"2.jpg"];
myImageView.layer.cornerRadius = view2.frame.size.width/2;
myImageView.layer.masksToBounds = YES;
return cell;
}
why is that? I'm confusing with the place where I should describe my view on my cell.
It seems you are using xib or storyboard, so it won't call initWithFrame: of UICollectionViewCell.
Using awakeFromNib instead:
-(void)awakeFromNib
{
[super awakeFromNib];
//Is myImageView an outlet of storyboard? if it is you needn't realloc a UIImageView instance
//myImageView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 200, 60, 60)];
myImageView.layer.cornerRadius = 5.0;
myImageView.layer.masksToBounds = YES;
//[self.contentView addSubview:myImageView];
}

Resources