1

I have a collection view and I subclassed the collection view cell so I have have repeating content.

Within my collection view class, I use this method to access and set the objects in my cell, for example set a label that is attached to a cell?

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{


CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];


//Configure Cell


//Set Name
DataManager *dm = [DataManager sharedInstance];
NSMutableArray *names = [dm objectForKey:@"nameArray"];
NSString *name = [names objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"%@", name]; //set cell here

    return cell;
}

However, I need to update the cell.textLabel, but I cannot access the cell.textLabel in other methods?

How can I access the cell.textLabel in other methods other than this one?

*note, I want to update ALL my cells, not just one cell.

Josh
  • 745
  • 1
  • 7
  • 22
  • When do you want to update the textLabel? – hoya21 Jul 10 '15 at 12:36
  • @hoya21 multiple times, as soon as the app receives a push notification I want a method to trigger the update of the textLabel, however I don't want to do `[collectionView reloadData];` because I have a transform applied to my cells which will reset the cell size (looks bad), this is why I just want to access the textLabel. – Josh Jul 10 '15 at 12:38
  • For reload single cell of UICollectionview you can refer to this link http://stackoverflow.com/a/20318136/3792386 – iOS Dev Jul 10 '15 at 13:10

3 Answers3

1

You can only access "visible" UICollectionViewCell, using visibleCells methods.

For those "invisible" cells, you have to configure them in collectionView:cellForItemAtIndexPath.

user3480295
  • 1,058
  • 15
  • 21
0

So i suggest you to keep an Array with indexPaths of the cells that you want to change and then every time that you want to change the text call a method and change them. Here it is a method that i use to change only one cell everytime that i want.

-(void) makePriorCell:(NSIndexPath *) indexPath{

UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];

//cell.backgroundColor = [UIColor greenColor];
UILabel *label = (UILabel *)[cell viewWithTag:101];

if (indexPath.item==0) {

    UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
    recipeImageView.image = [UIImage imageNamed:@"2A copy.png"];


}
else if (indexPath.item==1) {

    UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
    recipeImageView.image = [UIImage imageNamed:@"3A copy.png"];

}
else if (indexPath.item==2) {

    UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
    recipeImageView.image = [UIImage imageNamed:@"4A copy.png"];

}
else if (indexPath.item==3) {

    UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
    recipeImageView.image = [UIImage imageNamed:@"5A copy.png"];

}
else if (indexPath.item==4) {

    UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
    recipeImageView.image = [UIImage imageNamed:@"5a_Istoria_Perivallon copy.png"];

}
else if (indexPath.item==5) {

    UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
    recipeImageView.image = [UIImage imageNamed:@"7A copy.png"];

}
else if (indexPath.item==6) {

    UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
    recipeImageView.image = [UIImage imageNamed:@"6A copy.png"];

}
else if (indexPath.item==7) {

    UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
    recipeImageView.image = [UIImage imageNamed:@"8A copy.png"];

}
else if (indexPath.item==8) {

    UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
    recipeImageView.image = [UIImage imageNamed:@"9A copy.png"];

}
else if (indexPath.item==9) {

    UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
    recipeImageView.image = [UIImage imageNamed:@"10A copy.png"];

}
else if (indexPath.item==10) {

    UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
    recipeImageView.image = [UIImage imageNamed:@"11A copy.png"];


}
else if (indexPath.item==11) {

    UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
    recipeImageView.image = [UIImage imageNamed:@"12A copy.png"];


}


label.textColor = [UIColor blackColor];



cell.backgroundColor=[UIColor whiteColor];}

For my purpose i change the image, the text color and the background of the cell for a specified cell. You can do the same with the text of the cell that you want if you have a tag on it.

hoya21
  • 893
  • 7
  • 24
  • Thanks, but this is not what I need. I want to be able to update all my cells, what I really need is a way to access my subclassed `textLabel` variable so I can do exactly what I did in the code I provided. I need a way to recall that snipit of code. – Josh Jul 10 '15 at 13:06
0

Your CollectionViewCell is just the view, you should give it information from the model and update accordingly, as you are doing inside - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

What I'd suggested then, is to first update your model and then update your cells:

dm[@"nameArray"] = updated_info //Updated names
[DataManager sharedInstance] = ...//Update DataManager
collectionView.reloadData //Update all your cells

Also I'd be careful about calling DataManager *dm = [DataManager sharedInstance]; inside the cellForItemAtIndexPath: method. Maybe it'd be better to use dependency injection for this. You can see more about that here

fdiaz
  • 2,600
  • 21
  • 27