I'm writing my calendar using UICollectionView.
i don't know why this line come out
this is my code for create UICollectionView
#import "MonthTableView.h"
#import "MonthTableCollectionViewCell.h"
#implementation MonthTableView
static NSString *sCELLIDENTIFIER = #"cellidentifier";
#pragma mark - View Lifecycle
-(MonthTableView *)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_arr_Week = #[#"日",#"一",#"二",#"三",#"四",#"五",#"六",];
[self initSubView];
}
return self;
}
-(void)initSubView
{
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
flowLayout.minimumInteritemSpacing = 0;
flowLayout.minimumLineSpacing = 0;
UICollectionView *col_Month = [[UICollectionView alloc]initWithFrame:CGRectMake(5, 5, self.frame.size.width - 10, self.frame.size.height - 10) collectionViewLayout:flowLayout];
col_Month.dataSource = self;
col_Month.delegate = self;
[self addSubview:col_Month];
[col_Month registerClass:[MonthTableCollectionViewCell class] forCellWithReuseIdentifier:sCELLIDENTIFIER];
}
#pragma mark - UICollectionView Delegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 2;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
if (section == 0) {
return 7;
}
else {
return 5*7;
}
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat width = collectionView.frame.size.width / 7;
return CGSizeMake(width, width);
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MonthTableCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:sCELLIDENTIFIER forIndexPath:indexPath];
//Debug
if (indexPath.section == 0) {
cell.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1];
[cell.btn_Day setTitle:[NSString stringWithFormat:#"%#",_arr_Week[indexPath.row]] forState:UIControlStateNormal];
}
else
{
cell.btn_Day.backgroundColor = [UIColor whiteColor];
[cell.btn_Day setTitle:[NSString stringWithFormat:#"%ld",indexPath.row] forState:UIControlStateNormal];
}
return cell;
}
#end
And here is my Custom cell
#import "MonthTableCollectionViewCell.h"
#implementation MonthTableCollectionViewCell
-(MonthTableCollectionViewCell *)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self initSubViews];
}
return self;
}
-(void)initSubViews
{
_btn_Day = [UIButton buttonWithType:UIButtonTypeSystem];
_btn_Day.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
[self addSubview:_btn_Day];
}
#end
there is nothing in .h file but declaring vars
BTW i'm using Xcode6.
Related
My iOS application has not yet support iPhoneX layout.
That is my application doesn't have Launch Screen for iPhoneX (1124x2436, 2436x1124).
Thus on iPhoneX, UI my application is shown on 735x1334 area. It's OK.
Now I added UICollectionView on the top of my view.
I assigned the size of CollectionViewItem so that the CollectionView has 6 items horizontally.
On iPhone8(iOS11.2) the CollectionViewItems were allocated as I intended.
But on iPhoneX(iOS11.2) the CollectionView has 2 rows and first row is blank, so it's even scrollable.
I'd appreciate it if you would tell me the way to layout my collectionview as I intended.
My code is like below,
View Controller.m
- (void)loadView
{
self.view = [[FirstView alloc] init];
}
FirstView.m
- (id)init
{
self = [super init];
if (self) {
self.backgroundColor = UIColor.whiteColor;
[self initGridView];
}
return self;
}
- (void)initGridView
{
_gridView = [[GridView alloc] init];
[self addSubview:_gridView];
}
- (void)layoutSubviews
{
[super layoutSubviews];
[self layoutGridView];
}
- (void)layoutGridView
{
CGSize parentSize = _gridView.superview.frame.size;
_gridView.frame = CGRectMake(0, 0, parentSize.width, 48);
}
GridView.m
- (id)init
{
self = [super initWithFrame:CGRectZero collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
if (self) {
self.delegate = self;
self.dataSource = self;
[self registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class])];
self.backgroundColor = UIColor.grayColor;
}
return self;
}
#pragma mark -
#pragma mark UICollectionViewDataSource
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
NSString *cellName = NSStringFromClass([UICollectionViewCell class]);
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellName forIndexPath:indexPath];
if (indexPath.row % 2 == 0) {
cell.contentView.backgroundColor = UIColor.blueColor;
}
else {
cell.contentView.backgroundColor = UIColor.orangeColor;
}
return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 6;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGSize selfSize = self.frame.size;
CGFloat width = selfSize.width / 6;
return CGSizeMake(width, 48);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 0;
}
I've resolved the issue by adding the following line in my init() method of GridView.m file.
if #available(iOS 11.0, *) {
self.collectionView.contentInsetAdjustmentBehavior = .never
} else {
// Fallback on earlier versions
}
Thank you.
I have a UICollectionView in a custom UITableViewCell, only UIImageView in the UICollectionView. I use SDWebImage to load image.What I want is after loading the image, both UICollectionView and it's contentView(UITableViewCell) update themselves' height to show all the image. For example, if I have three images in UICollectionView, I hope the collectionview's height equals to all three images' height summation.
Here is some code:
TableView:
- (void)createPostDetailUI{
_postDetailTableView = [UITableView new];
_postDetailTableView.delegate = self;
_postDetailTableView.dataSource = self;
_postDetailTableView.estimatedRowHeight = 180;
_postDetailTableView.tableFooterView = [UIView new];
[_postDetailTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
_postDetailTableView.rowHeight = UITableViewAutomaticDimension;
[self makeConstraints];
}
- (void)makeConstraints{
[_postDetailTableView mas_makeConstraints:^(MASConstraintMaker *make) {
UIEdgeInsets padding = UIEdgeInsetsMake(0, 0, 50, 0);
make.edges.equalTo(self.view).insets(padding);
}];
}
- (void)requestPostData{
//some request code
[_commentsArray addObjectsFromArray:[PostCommentModel arrayWithResponseObject:responseObject][0]];
[_postDetailTableView reloadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _commentsArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
PostDetailHeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"PostDetailHeaderTableViewCell" forIndexPath:indexPath];
cell = [[PostDetailHeaderTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"PostDetailHeaderTableViewCell"];
[cell updateCellWithModel:_postModel];
return cell;
}
UITableViewCell(UICollectionView inside it):
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
//some other views
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
layout.minimumLineSpacing = 4.0;
layout.estimatedItemSize = CGSizeMake(300, 300);
_postImageCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
_postImageCollectionView.backgroundColor = [UIColor clearColor];
_postImageCollectionView.showsHorizontalScrollIndicator = NO;
_postImageCollectionView.autoresizesSubviews = YES;
[_postImageCollectionView registerClass:[PostDetailImageCollectionViewCell class] forCellWithReuseIdentifier:#"PostDetailImageCollectionViewCell"];
_postImageCollectionView.delegate = self;
_postImageCollectionView.dataSource = self;
[self.contentView addSubview:_postImageCollectionView];
[self makeConstraints];
}
return self;
}
- (void)makeConstraints{
//some other views' autolayout code
[_postImageCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(_postContentLabel);
make.right.equalTo(_postContentLabel);
make.top.equalTo(_postContentLabel.mas_bottom).offset(14);
make.height.greaterThanOrEqualTo(#20);
make.bottom.equalTo(self.contentView).offset(-16);
}];
}
- (void)updateCellWithModel:(PostsModel *)model{
_imageArray = [model getImageList];
[_postImageCollectionView reloadData];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return _imageArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
PostDetailImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"PostDetailImageCollectionViewCell" forIndexPath:indexPath];
NSString *cellImageURL = _imageArray[indexPath.row];
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:cellImageURL] placeholderImage:[UIImage imageNamed:#"icon_default"]];
return cell;
}
UICollectionViewCell:
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
_imageView = [UIImageView new];
_imageView.frame = self.contentView.frame;
_imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.contentView addSubview:_imageView];
}
return self;
}
I use auto layout and use masonry to manage it. But whatever I try to fix it, it just show me 20pt height.
ps:How to make CollectionViewCell frame fit image frame
Try this to calculate height
CGheight height= [self.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height+2;
add this in ur custom cell and call this from collectionview delegate method
I have a UITabBarController that contains 2 UIViewControllers. The 2nd UIViewController crashes when ever I try to show from the 1st UIViewController.
The 2nd UIViewController crashes because of its UICollectionView declared as a private property.
I get a EXC_BAD_ACCESS so I think that the 2nd UIViewController tries to do [self setCollectionView] but when its property self.collectionView is not init yet (still nil).
I don't understand why it behaves so - I have no problem implementing the UICollectionView the same way in the 1st UIViewController. Here is the .m file of the 2nd UIViewController :
#interface WorkoutViewController () <UICollectionViewDataSource, UICollectionViewDelegate, DAPageControlViewDelegate>
#property (strong, nonatomic) UICollectionView *collectionView;
#property (strong, nonatomic) DAPageControlView *pageControlView;
#end
#implementation WorkoutViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self createCollectionView];
[self createPageView];
// Constraints
// CollectionView
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
// PageControl
[self.pageControlView mas_makeConstraints:^(MASConstraintMaker *make) {
make.trailing.equalTo(self.view);
make.leading.equalTo(self.view);
make.height.equalTo(screenAdjustedSizeFrom(15));
make.bottom.equalTo(self.view).offset(screenAdjustedSizeFrom(-15).floatValue);
}];
// Wake up collectionView
[self.collectionView reloadData];
}
-(void)createCollectionView {
// Layout
UICollectionViewFlowLayout *collectionViewLayout = [[UICollectionViewFlowLayout alloc] init];
collectionViewLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
// UICollectionView
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:collectionViewLayout];
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
[self.view addSubview:self.collectionView];
// Behavior
self.collectionView.pagingEnabled = YES;
// Appearance
self.collectionView.backgroundColor = [Color colorWithName:nil alpha:0.2f];
self.collectionView.showsHorizontalScrollIndicator = NO;
// Register cells
//[self.collectionView registerClass:[TrackingSetCollectionViewCell class] forCellWithReuseIdentifier:TrackingSetCollectionViewCellIdentifier]; }
-(void)createPageView {
// Support for pagination - DAPageControlView
self.pageControlView = [[DAPageControlView alloc] initWithFrame:CGRectZero];
self.pageControlView.delegate = self;
[self.view addSubview:self.pageControlView];
self.pageControlView.hidden = YES; // do not show the dots
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return fetchManagedObjectsFromEntity(#"Autor", #[[NSSortDescriptor sortDescriptorWithKey:#"autorID" ascending:YES]], nil, self.managedObjectContext).count + 1; // +1 for testing !
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
//test
UICollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:#"cell" forIndexPath:indexPath];
if (indexPath.row==0) {
cell.backgroundColor = [UIColor blueColor];
}
if (indexPath.row==1) {
cell.backgroundColor = [UIColor redColor];
}
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return self.collectionView.bounds.size;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 0.0;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(0, 0, 0, 0);
}
#pragma mark - DAPageControlView Delegate
- (void)pageControlViewDidChangeCurrentPage:(DAPageControlView *)pageControlView
{
[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:pageControlView.currentPage inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
}
Problem solved - I forgot to register a class for the UICollectionViewCell. I thought it was not necessary for to do so for the 'default' class UICollectionViewCell but since the UICollectionView is added programmatically, it makes sense.
I have a UICollectionView that is using the Flow Layout. At this point I'm not really doing anything special. I'm just programmatically adding the view and the UICollectionViewCell.
When you touch a cell it is registering the touch event for the cell that is located above it. So if you touch a cell on row 2 the the cell above is the one that actually registers the touch.
Here is the code that I have setting things up.
#interface KFYSearch() <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UISearchBarDelegate>
#property(nonatomic, strong)UISearchBar *searchBar;
#property(nonatomic, strong)UICollectionView *collectionView;
#property(nonatomic, strong)NSArray *collectionData;
#end
#implementation KFYSearch
-(id)initWithView:(UIView*)view
{
self = [super init];
if(self)
self.view = view;
return self;
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if(_searchBar == nil)
{
self.view.frame = CGRectMake(20, 20, self.view.superview.frame.size.width - 40, self.view.superview.frame.size.height - 40);
[self.view setBackgroundColor:[UIColor whiteColor]];
[self createChildViews];
}
}
-(void)createChildViews
{
_searchBar = [[UISearchBar alloc] initWithFrame: CGRectMake(5, 5, self.view.frame.size.width - 10, 30)];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(_searchBar.frame.origin.x, _searchBar.frame.origin.y + (_searchBar.frame.size.height + 2), _searchBar.frame.size.width, self.view.frame.size.height - ((_searchBar.frame.origin.y + _searchBar.frame.size.height) + 10)) collectionViewLayout:flowLayout];
_collectionView.dataSource = self;
_collectionView.delegate = self;
[_collectionView registerClass:[KFYKlickableCell class] forCellWithReuseIdentifier:#"Cell"];
[_collectionView setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:_searchBar];
[self.view addSubview:_collectionView];
[self getData];
}
-(void)getData
{
KFYGetKlickablesAction *getKlickablesAction = [[KFYGetKlickablesAction alloc]init];
_collectionData = [getKlickablesAction mockData];
[_collectionView reloadData];
}
#pragma UICollectionView Delegate stuff;
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return _collectionData.count;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
KFYKlickableCell *cell= [_collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
[cell setBackgroundColor:[UIColor redColor]];
KFYKlickableVO *vo = [_collectionData objectAtIndex:indexPath.row];
[cell renderKlickable:vo];
return (UICollectionViewCell*)cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(60, 60);
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(5, 5, 5, 5);
}
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
KFYKlickableVO *vo = [_collectionData objectAtIndex:indexPath.row];
NSLog(vo.tag);
}
#pragma UITextField Delegate stuff
-(void)textFieldDidBeginEditing:(UITextField *)textField {
//Keyboard becomes visible
self.view.frame = CGRectMake(self.view.frame.origin.x,
self.view.frame.origin.y,
self.view.frame.size.width,
self.view.frame.size.height - 215 + 50); //resize
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
//keyboard will hide
self.view.frame = CGRectMake(self.view.frame.origin.x,
self.view.frame.origin.y,
self.view.frame.size.width,
self.view.frame.size.height + 215 - 50); //resize
}
#end
I have a some collectionViews and one table view that are in the same view controller. The rather strange problem is that when i scroll up-down I alway get memory increases.
Instruments show a lot of allocation of VM:CoreAnimation objects but I can't track them down (they are inside collectionView itself).
prepare for reuse is getting called inside the cells, I checked this.
Here is the code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.receivedChannels = NO;
self.ItemsDict = [NSMutableDictionary new];
self.crtBatchSet = [NSMutableIndexSet new];
if ([[PSDeviceInfo sharedInstance] is_iPad]) {
self.epgWidth = EPG_WIDTH_IPAD;
} else {
self.epgWidth = EPG_WIDTH_IPHONE;
}
crtBatchSetSize = 0;
self.currentSelecteIndexOfDateCell = 0;
//size
self.widthDictionary = [NSMutableDictionary new];
self.centerXDictionary = [NSMutableDictionary new];
self.layout = [[MultipleLineLayout alloc] initWithWidthDictionary:self.widthDictionary andCenterXDictionary:self.centerXDictionary];
self.ItemsCollectionVIew.collectionViewLayout = self.layout;
self.ItemsCollectionVIew.showsHorizontalScrollIndicator = NO;
self.ItemsCollectionVIew.showsVerticalScrollIndicator = NO;
self.layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
[self.ItemsCollectionVIew registerClass:[ItemColectionCell class] forCellWithReuseIdentifier:#"ItemColectionCellID"];
[self.hoursCollectionView registerClass:[HoursCollectionViewCell class] forCellWithReuseIdentifier:#"HoursColectionCellID"];
[self.datePickerCollectionView registerClass:[DatePickerCollectionViewCell class] forCellWithReuseIdentifier:#"DatePickerColectionCellID"];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (scrollView == self.ItemsCollectionVIew){
self.channelsTableView.contentOffset = CGPointMake(0, self.ItemsCollectionVIew.contentOffset.y);
self.hoursCollectionView.contentOffset = CGPointMake(self.ItemsCollectionVIew.contentOffset.x, 0);
}
if (scrollView == self.channelsTableView) {
self.ItemsCollectionVIew.contentOffset = CGPointMake(self.ItemsCollectionVIew.contentOffset.x,self.channelsTableView.contentOffset.y);
}
if (scrollView == self.hoursCollectionView) {
self.ItemsCollectionVIew.contentOffset = CGPointMake(self.hoursCollectionView.contentOffset.x,self.ItemsCollectionVIew.contentOffset.y);
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *channelCellID = #"ChannelTableCellID";
ChannelTableCell *cell = [tableView dequeueReusableCellWithIdentifier:channelCellID forIndexPath:indexPath];
cell.myIndexInTable = indexPath.row;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell getData];
return cell;
}
#pragma mark - UITableViewDelegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.ItemsCollectionVIew reloadData];
}
#pragma mark - UICollectionViewDataSource Methods
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
if (collectionView == self.datePickerCollectionView || collectionView == self.hoursCollectionView) {
return 1;
}else{
if (self.receivedChannels) {
return totalNoChannels;
} else {
return 1;
}
}
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
if (collectionView == self.datePickerCollectionView) {
return DELTA_DAYS + 1;
}else if (collectionView == self.hoursCollectionView) {
return 48; //24 hours * 2
}else{
// if (collectionView == self.ItemsCollectionVIew){
NSArray *sectionItems = self.ItemsDict[#(section)];
if (sectionItems) {
// return sectionItems.count;
return 100;
} else {
return 100;
}
}
return 1;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
if (collectionView == self.datePickerCollectionView) {
static NSString *ItemCellID = #"DatePickerColectionCellID";
DatePickerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ItemCellID forIndexPath:indexPath];
[self setDayAndDateforCell:cell at:indexPath];
[cell setBackgroundColor:[self getColorForCell:self.currentSelecteIndexOfDateCell == indexPath.row ? YES:NO]];
return cell;
}else if (collectionView == self.hoursCollectionView){
static NSString *ItemCellID = #"HoursColectionCellID";
HoursCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ItemCellID forIndexPath:indexPath];
cell.hourLabel.text = [self getTimeLineCellValuerFor:indexPath];
return cell;
} else{
// if (collectionView == self.datePickerCollectionView) {
static NSString *ItemCellID = #"ItemColectionCellID";
ItemColectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ItemCellID forIndexPath:indexPath];
NSArray *Items = self.ItemsDict[#(indexPath.section)];
if (indexPath.row >= Items.count) {
return cell;
}
MvpItem *prog = Items[indexPath.row];
[cell updateInfo:prog];
[cell setBackgroundColor:[self getColorForCell:NO]];
return cell;
}
}
#pragma mark - UICollectionViewDelegate Methods
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
if (collectionView == self.datePickerCollectionView) {
self.currentSelecteIndexOfDateCell = indexPath.row;
}
if (collectionView == self.ItemsCollectionVIew) {
[self pushDetailsatIndexPath:indexPath];
}
}
The code for the tableView cell is:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
[self.layer setBorderColor:[UIColor blackColor].CGColor];
[self.layer setBorderWidth:1.0f];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)prepareForReuse{
self.thumbnailImage.image = nil;
self.channel = nil;
[self clearDelegate];
[super prepareForReuse];
}
- (void)dealloc
{
[self clearDelegate];
}
- (void)clearDelegate
{
NSString *url = self.channel.media_content.thumbPoster.url;
// [[ImageManager sharedInstance] removeDelegate:self forImgUrl:url];
[[TVManager sharedInstance] removeDelegate:self];
}
- (void)getData
{
[[TVManager sharedInstance] getChannelAndItemssForIndex:self.myIndexInTable forDateDelta:0 forDelegate:self];
}
- (void)didReceiveTotalNoChn:(NSInteger)totalChnNo{
return;
}
- (void)didReceiveChannel:(MvpChannel *)channel withItemss:(NSArray *)Itemss forDateDelta:(NSInteger)date withIndex:(NSUInteger)index{
if (index != self.myIndexInTable) {
return;
}
if ([self.channel isEqual:channel]) {
return;
}
self.channel = channel;
NSString *imgUrl = self.channel.media_content.thumbPoster.url;
// UIImage *img = [[ImageManager sharedInstance] getImageForUrl:imgUrl forIndex:self.myIndexInTable withDelegate:self];
// if (img) {
// self.thumbnailImage.image = img;
// }
}
#pragma mark - Image Delegate
- (void)didReceiveImage:(UIImage *)image forIndex:(NSInteger)index
{
if (index != self.myIndexInTable) {
return;
}
if (image) {
self.thumbnailImage.contentMode = UIViewContentModeScaleAspectFit;
self.thumbnailImage.image = image;
[self setNeedsLayout];
}
}
And inside the collectionViewCell:
#interface ItemColectionCell ()
#property (retain, nonatomic) UILabel *titleLabel;
#property (retain, nonatomic) UILabel *genreLabel;
#property (retain, nonatomic) UILabel *intervalLabel;
#end
#implementation ItemColectionCell //collection
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self.layer setBorderColor:[UIColor blackColor].CGColor];
[self.layer setBorderWidth:1.0f];
self.titleLabel = [[UILabel alloc] init];
self.genreLabel = [[UILabel alloc] init];
self.intervalLabel = [[UILabel alloc] init];
[self.titleLabel setBackgroundColor:[UIColor clearColor]];
[self.genreLabel setBackgroundColor:[UIColor clearColor]];
[self.intervalLabel setBackgroundColor:[UIColor clearColor]];
[self.titleLabel setTextColor:[UIColor whiteColor]];
[self.genreLabel setTextColor:[UIColor whiteColor]];
[self.intervalLabel setTextColor:[UIColor whiteColor]];
[self.titleLabel setFont:[UIFont fontWithName:#“cf-Bold" size:15]];;
[self.genreLabel setFont:[UIFont fontWithName:#“cf-Regular" size:11]];;
[self.intervalLabel setFont:[UIFont fontWithName:#“cf-Regular" size:11]];;
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
- (void)prepareForReuse{
self.titleLabel.text = nil;
self.genreLabel.text = nil;
self.intervalLabel.text = nil;
[self.titleLabel removeFromSuperview];
[self.genreLabel removeFromSuperview];
[self.intervalLabel removeFromSuperview];
[super prepareForReuse];
}
- (void)updateInfo:(MvpItem*)item{
if (!item) {
return;
}
NSDateFormatter *startTimeFormat = [[NSDateFormatter alloc] init];
[startTimeFormat setDateFormat:#"hh:mm"];
NSDateFormatter *endTimeFormat = [[NSDateFormatter alloc] init];
[endTimeFormat setDateFormat:#"hh:mm a"];
NSString *startTime = [startTimeFormat stringFromDate:item.startTime];
NSString *endTime = [endTimeFormat stringFromDate:item.endTime];
self.titleLabel.frame = CGRectMake(10, 7, self.frame.size.width - 10, 15);
self.genreLabel.frame = CGRectMake(10, 20, self.frame.size.width - 10, 15);
self.intervalLabel.frame = CGRectMake(10, 32, self.frame.size.width - 10, 15);
self.titleLabel.text = item.title;
self.genreLabel.text = #“tewst”;
self.intervalLabel.text = [NSString stringWithFormat:#"%# - %#", startTime, endTime];
[self addSubview:self.titleLabel];
[self addSubview:self.genreLabel];
[self addSubview:self.intervalLabel];
}
What ca I do to solve this problem?
Did you try to remove the cell or heavy tasks triggered by displaying that cell when it did end displaying?
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
or
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath