I've got an issue with my UICollectionView where my cells are always initiated blank/in a default state with no data.
The data only appears in the cells after scrolling them in and out of view.
Code:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"EquipmentCell";
APInventoryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
if (!cell) cell = [[APInventoryCollectionViewCell alloc] init];
//set cell data...
cell.label.text = [arrayOfStrings objectAtIndex:indexPath.row];
return cell;
}
Your problem is that you probably set the arrayOfStrings somewhere in the viewDidLoad method of your view controller, the problem with that is that the collectionview datasource calls are done before that.
What you should do in your viewDidLoad method just call [collectionview reloadData]; an you will be fine
Related
I have a collection view that in which a cell is populated based on an array of int's.
After the cells are created I want to check for parameters for each cell via network request.
At the moment I carry out the network request for each cell in this method and it causes a cell to be created before its parameters are assigned due to inconsistent networks.
Is there a method to run a network function (sendGetPar:) on completion of laying out the cells? Obviously this can then be reused when the user scrolls etc.
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
BOOL isFocusOn = [_userDefault boolForKey:#"mixFocusOn"];
if (isFocusOn == TRUE) {
CDCChannelStrip *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cell" forIndexPath:indexPath];
NSNumber *setChan = [self.focusChannels objectAtIndex:indexPath.section];
NSInteger chanInt = [setChan intValue] +1;
cell.clipsToBounds = YES;
[cell initData:(chanInt)];
[self.mixMonitorView setChannelsStripToType:(cell)];
[self.mixMonitorView sendGetPar:chanInt];
return cell;
}
I believe the Apple's documentation would be helpful
probably the methods:
collectionView:didEndDisplayingCell:forItemAtIndexPath:
Tells the delegate that the specified cell was removed from the collection view.
collectionView:didUpdateFocusInContext:withAnimationCoordinator:
Tells the delegate that a focus update occurred.
Something like this:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
BOOL isFocusOn = [_userDefault boolForKey:#"mixFocusOn"];
if (isFocusOn == TRUE) {
CDCChannelStrip *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cell" forIndexPath:indexPath];
NSInteger chanInt = indexPath.row +1;
cell.clipsToBounds = YES;
[cell initData:(chanInt)];
[self.mixMonitorView setChannelsStripToType:(cell)];
[self.mixMonitorView sendGetPar:chanInt];
return cell;
}
-(void) sendGetPar:(NSInteger)index // you could return the parameter via this method.
{
NSLog(#"Parameter:%ld", (long)index);
}
I have a UICollectionView. When an user clicks a cell the colour of the cell changes to red. I have coded the above part and it works perfectly.
However, when I scroll below, other cells in the collection view has got highlighted with the colour red. I am sure that it hasn't been selected but only highlighted.
How can I solve this.
My code is as follows:
cellForItemAtIndexPath Method
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"cell";
ViewCell *vc = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
vc.vciv.image = [arrayOfImages objectAtIndex:indexPath.row];
return vc;
}
didSelectItemAtIndexPath Method
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if([items containsObject:allItem[indexPath.row]] ){
UICollectionViewCell *c =[collectionView cellForItemAtIndexPath:indexPath];
c.backgroundColor = [UIColor clearColor];
} else {
UICollectionViewCell *c =[collectionView cellForItemAtIndexPath:indexPath];
c.backgroundColor = [UIColor redColor];
}
As the collection view cells get reused, you need to make sure a dequeued cell is not highlighted in the cellForItemAtIndexPath: method:
UICollectionViewCell *vc = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
vc.backgroundColor = [UIColor clearColor];
This code will clear the cell's background always when dequeuing. If you need to preserve the highlighting, you need to store the indexPaths of the cells that you want to have highlighted, and check for the current cell being in that list.
EDIT
It seems that your items variable is well suited to do that.
So you would do:
UICollectionViewCell *vc = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
vc.backgroundColor = [items containsObject:allItem[indexPath.row]] ? [UIColor redColor] : [UIColor clearColor];
I have my data source methods
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 10;
}
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
LPDiaryCollectionViewCell *cell = (LPDiaryCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:#"cell" forIndexPath:indexPath];
cell.testLabel.text = #"test";
return cell;
}
The cell is registered
[self.collectionView registerClass:[LPDiaryCollectionViewCell class] forCellWithReuseIdentifier:#"cell"];
Datasource, delegate and outlet are connected
But when I run, datasource methods don't get called and view is plain white with no UICollectionView showing.
EDIT: added the uicollectionview programattically instead. It displays but still no cell.
You have to set cell identifier for collection view cell in storybord(Ex. I have declared identifier = "ReuseCell") and used same storyboard identifer in cellForItemAtIndexPath method.
static NSString *identifier = #"ReuseCell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
static NSString *identifier = #"Yourcellname";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
There is any code like this one that work with UICollectionViewCell when the method reuse ?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellId = [NSString stringWithFormat:#"CellId%d%d",indexPath.row,indexPath.section];
if (!cell)
{
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellId] autorelease];
}
return cell;
}
The whole point is to reuse cells, that's why the reuse identifier should be the same for all cells, at least all cells of one class (that's why it makes sense to declare CellId as a static variable - this method will be called a lot). dequeueReusableCellWithReuseIdentifier: method returns cell ready to be reused, if any. If there is no such cell you should create it and later, when it is no longer visible UICollectoinView will add it into "reusable cells pool" and return for dequeueReusableCellWithReuseIdentifier:.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellId = #"YourCellIdentifier";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellId];
if (!cell) {
cell = [[CustomCell alloc] initWithFrame:yourFrame];
}
return cell;
}
Yes, there is a similar method for collectionview as shown below:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"collectionCell";
collectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
return cell;
}
Yes, there is:
UICollectionReusableView *collView = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
You can find the info in the docs:
https://developer.apple.com/library/ios/documentation/uikit/reference/UICollectionView_class/Reference/Reference.html#//apple_ref/occ/instm/UICollectionView/dequeueReusableCellWithReuseIdentifier:forIndexPath:
I have a collection view in my app, I want it to contain a custom cells. I've created a custom cell view xib file. Then I use it in my data source method :
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
OtherCustomersCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:OTHER_CUSTOMERS_CELL_IDENTIFIER forIndexPath:indexPath];
if(cell == nil){
NSArray *nsObjects = [[NSBundle mainBundle] loadNibNamed:#"OtherCustomersCell" owner:nil options:nil];
for(id obj in nsObjects)
if([obj isKindOfClass:[OtherCustomersCell class]])
cell = (OtherCustomersCell*) obj;
}
[cell.name setText:#"AAAA BBBBB"];
return cell;
}
But when I run the app, there is just a black rectangular where the collection view should be (at the bottom under the table view):
What am I doing wrong?
Thank you in advance.
Collection views work differently than table views in that you don't have to create a cell if one can't be dequeued.
Instead, you have to register the nib for the cell first:
- (void)viewDidLoad
{
...
UINib *cellNib = [UINib nibWithNibName:#"OtherCustomersCell" bundle:nil];
[collectionView registerNib:cellNib forCellWithReuseIdentifier:OTHER_CUSTOMERS_CELL_IDENTIFIER];
}
You can then dequeue the cell and it will be created automatically for you if necessary:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
OtherCustomersCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:OTHER_CUSTOMERS_CELL_IDENTIFIER forIndexPath:indexPath]; // cell won't be nil, it's created for you if necessary!
[cell.name setText:#"AAAA BBBBB"];
return cell;
}
You have to register UICollectionView instance in the following way in viewdidload then you can use this.
[self.photoListView registerNib:[UINib nibWithNibName:#"UIcollectionViewCell" bundle:nil] forCellWithReuseIdentifier:#"Identifier"];