[UICollectionView performSelector:withObject:]: message sent to deallocated instance - ios

I am having problem with my iOS app which I am not sure what is the root cause of it even after I've turned on NSZombie to debug.
Already tried to google with reliable source but still couldn't get any answer.
For my iOS App, I am using MMDrawerController Library as the left slide menu. Which I am having a problem when I switching view from left menu, if the page contains UICollectionView, it will crash with the message below.
[UICollectionView performSelector:withObject:]: message sent to deallocated instance
The view load smooth for the first time, but when I attempt to switch back from another view, it crashed with the error above.
Here are my UICollectionView Data Source and cell builder functions
#pragma mark: UICollectionViewCell Builder
- (ItemNormalCVCell *)buildItemNormalCVCell:(NSIndexPath *)indexPath
{
ItemNormalCVCell *cell = [self.mCollectionView dequeueReusableCellWithReuseIdentifier:#"CVCellItemNormal" forIndexPath:indexPath];
return cell;
}
- (LoadingCVCell *)buildLoadingCVCell:(NSIndexPath *)indexPath
{
LoadingCVCell *cell = [self.mCollectionView dequeueReusableCellWithReuseIdentifier:#"CVCellLoading" forIndexPath:indexPath];
return cell;
}
#pragma mark: UICollectionView DataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
NSInteger itemsCount = self.items.count;
if( self.isLoading_.boolValue )
return itemsCount + 1;
return itemsCount;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger itemsCount = self.items.count;
if( self.isLoading_.boolValue ){
if( indexPath.row == itemsCount )
return [self buildLoadingCVCell:indexPath];
}
return [self buildItemNormalCVCell:indexPath];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat cvWidth = (self.view.frame.size.width / 2) - 1.f;
return CGSizeMake(cvWidth, 300.f);
}
- (UIEdgeInsets)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(0, 0, 0, 1.f); // top, left, bottom, right
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 1.f;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 1.f;
}

Related

iOS Define Number of Cell according to Page in UICollectionview (Collectionview with Paging)

I am working on Collectionvew with Paging . I am Successfully able to done collectionview with Paging. and It's work perfectly.
But the problem is i need only 10 cell in 1 page , then another 10 cell 2nd page and Next 10 cell in 3rd page. but I dont know How can i achive it when page changed.And i also want to change the title when page changed like , Easy ,Medium and Hard(Levels).
Currently i take 30 cells in collectionview . Here is my methods for collectionview ,
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{
return kNumberOfCells;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"GameCollectionViewCell";
GameCollectionViewCell *cell = (GameCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
if (cell == nil)
{
NSArray *xib = [[NSBundle mainBundle] loadNibNamed:#"HomeCollectionViewCell" owner:self options:nil];
cell = [xib objectAtIndex:0];
}
cell.lblLevel.text=[NSString stringWithFormat:#"%d",indexPath.item+1];
cell.lblLevelPoints.text=#"points";
return cell;
}
#pragma mark – UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
float width = collectionView.frame.size.width/6.4;//120
float height = collectionView.frame.size.height/4;//120
return CGSizeMake(width,width);
}
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(8.0, 14.0, 5.0, 14.0);//TOP , LEFT ,BOTTOM ,RIGHT
}
Output Currently i am getting :-
There are two solutions :
Solution 1 :
You can take number of section 3. Each section with 10 items. With each section you have change current page of UIPageControl and change title too.
Solution 2:
Place PageControl in your view or set by Code.
Set UIScrollViewDelegate
In Collectionview-> cellForItemAtIndexPath (Method) add the
below code for calculate the Number of pages,
int pages =floor(ImageCollectionView.contentSize.width/ImageCollectionView.frame.size.width);
[pageControl setNumberOfPages:pages];
Add the ScrollView Delegate method
pragma mark - UIScrollVewDelegate for UIPageControl
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
CGFloat pageWidth = ImageCollectionView.frame.size.width;
float currentPage = ImageCollectionView.contentOffset.x / pageWidth;
if (0.0f != fmodf(currentPage, 1.0f))
{
pageControl.currentPage = currentPage + 1;
}
else
{
pageControl.currentPage = currentPage;
}
NSLog(#"finishPage: %ld", (long)pageControl.currentPage);
}
Reference Link :
Collectionview + Pagecontrol
Update:
There is one sample code also made by me on Github. It help you :
https://github.com/maniyarpayal/PagingWithCollectionView
try this delegate methods,
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake((self.view.frame.size.width / 5) , self.view.frame.size.height / 2);
}
- (NSUInteger)maximumNumberOfColumnsForCollectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout
{
return 5;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 0.0;
}
- (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);
}
i have also same problem, then use the above methods its will working for me, hope its helpful.
you can use this delegate method but you have to customise according to your requirement because i did not mention distance between cells and x-origin .and you have to do minus those distance from view width. hope this will help you.
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake((self.view.frame.size.width / 10) , self.view.frame.size.height / 2);
}

ios YTPlayerView does not respond to touch

I have a problem with YTPlayerView, I created a collection view, and in each cell is added subview YTPlayerView, the problem is that it shows great, but does not respond to pressing on view etc.
Not working on thumbnail page, when video playing all's fine.
How it's looking.
#pragma mark UICollectionView
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.dataSource.count;
}
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath{
YTCollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:self.identifierDataSource[indexPath.row]
forIndexPath:indexPath];
if (!cell) {
cell = [YTCollectionViewCell new];
}
if (!cell.loaded) {
[cell.playerView loadWithVideoId:self.dataSource[indexPath.row]];
cell.loaded = YES;
}
return cell;}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height);
}
-(void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
YTCollectionViewCell* ycell = (YTCollectionViewCell*)cell;
[ycell.playerView stopVideo];}
-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
YTCollectionViewCell* ycell = (YTCollectionViewCell*)cell;
if (!ycell.loaded) {
[ycell.playerView loadWithVideoId:self.dataSource[indexPath.row]];
ycell.loaded = YES;
}else{
//[ycell.playerView playVideo];
}
}
`

How to add Load more cell in UICollectionView?

I want to add load more cell in UICollectionview ? can anyone tell me how can i add load button ? i have created collectionview that works fine but i want to add Load more button in bottom of collection view cell like this
Here's my collection view
#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 3;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
switch (section) {
case 0: return 66;
case 1: return 123;
}
return 31;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
NSUInteger count = [self.stockImages count];
if (row == count) {
//Add load more cell
}
DemoCellView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[DemoCellView reuseIdentifier] forIndexPath:indexPath];
// Configure the cell
cell.titleLabel.text = [NSString stringWithFormat:#"%ld", (long)indexPath.item + 1];
NSLog(#"%#", self.stockImages[indexPath.item % self.stockImages.count]);
cell.imageView.image = self.stockImages[indexPath.item % self.stockImages.count];
return cell;
}
#pragma mark <DemoLayoutDelegate>
- (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
// Do something
// Insert new cell after clicking load more data
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout: (UICollectionViewLayout *)collectionViewLayout heightForHeaderInSection:(NSInteger)section {
return kFMHeaderFooterHeight;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout heightForFooterInSection:(NSInteger)section {
return kFMHeaderFooterHeight;
}
You can Add a footer
- (UICollectionReusableView *)collectionView:(JSQMessagesCollectionView *)collectionView
viewForSupplementaryElementOfKind:(NSString *)kind
atIndexPath:(NSIndexPath *)indexPath
{
if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
//load your footer you have registered earlier for load more
[super dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter
withReuseIdentifier:#“load more footer”
forIndexPath:indexPath];
}
return nil;
}
In your cellForItemAtIndexPath method you can check this:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
ImageCollectionViewCell *cellImage;
AddMoreCollectionViewCell *addMoreCell;
if(indexPath.row < [_dataSource numberOfItems]){
cellImage = [collectionView dequeueReusableCellWithReuseIdentifier:ImageCollectionCellIdentifier
forIndexPath:indexPath];
[cellImage configureForMediaViewModel:[_dataSource mediaViewModelForItemIndex:indexPath.row] delegate:self];
return cellImage;
}else{
addMoreCell = [collectionView dequeueReusableCellWithReuseIdentifier:AddMoreCollectionCellIdentifier
forIndexPath:indexPath];
addMoreCell.delegate = self;
return addMoreCell;
}
}
where ImageCollectionViewCell is the main kind of cells and AddMoreCollectionViewCell is a cell with a plus ('+') symbol and other stuff.
With this method, AddMoreCollectionViewCell always add at end of your collection view.
Hope it helps!

could not dequeue a view of kind: UICollectionElementKindCell with identifier

I have UICollectionView, but it seems that I set up everything right. But I get the error:
'could not dequeue a view of kind: UICollectionElementKindCell with identifier PeopleCellForList - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
My storybord:
My code is:
#interface PeopleCellForList : UICollectionViewCell
#end
#implementation PeopleCellForList
#end
#pragma mark - UICollectionView Datasource
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
return self.arrayPeople.count;
}
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:#"PeopleCellForList " forIndexPath:indexPath];
return cell;
}
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
}
#pragma mark – UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGFloat width = 106;
CGFloat height = width;
if (indexPath.row % 3 == 2) {
width = 108;
}
return CGSizeMake(width, height);
}
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(0, 0, 0, 0);
}
I tried [self.collectionViewMain registerClass:[PeopleCellForList class] forCellWithReuseIdentifier:#"PeopleCellForList"] in viewDidLoad: (during that I tried to remove and not remove cell from storyboard), but that didn't help.
You have an extra space in #"PeopleCellForList "

iOS: Scroll one cell at a time

I have horizontal scrolling set up now I would like for it to sort of scroll/snap one cell at a time when scrolling side to side.
Could someone shed some light? Here's the code I have so far:
- (void)viewDidLoad
{
[super viewDidLoad];
}
#pragma mark - UICollectionView Datasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 1;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 20;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MyCell *cell = [cv dequeueReusableCellWithReuseIdentifier:#"MyCell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
#pragma mark - UICollectionViewDelegateFlowLayout
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(50, 20, 50, 20);
}
[collectionView setPagingEnabled:YES]; might do the trick?
http://adoptioncurve.net/archives/2013/04/creating-a-paged-photo-gallery-with-a-uicollectionview/

Resources