I create a cell using this func:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
BOOL isFocusOn = [_userDefault boolForKey:#"mixFocusOn"];
CDCChannelStrip *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cell" forIndexPath:indexPath];
if (isFocusOn == TRUE) {
NSNumber *setChan = [self.focusChannels objectAtIndex:indexPath.section];
NSInteger chanInt = [setChan intValue];
[cell initData:(chanInt)];
[self getParameters:(setChan)];
} else {
NSInteger chanInt = indexPath.section;
NSNumber *chanNum = [NSNumber numberWithInt:chanInt]; // doesnt matter
[cell initData:(chanInt)]; // init
[self getParameters:(chanNum)]; // params
}
[self.mixMonitorView setChannelsStripToType:(cell)];
cell.clipsToBounds = YES;
return cell;
}
The func in question is initData which looks like this:
- (void)initData:(NSInteger)channel {
self.isBus = false;
self.isSendChan = false;
self.recallSafeFade = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"recallSafe"]];
self.recallSafeFade.y = 80;
[self.recallSafeFade setUserInteractionEnabled:YES];
self.recallSafeHead = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"recallSafeHeadamp"]];
[self.recallSafeHead setUserInteractionEnabled:NO];
self.recallSafePan = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"recallSafePan"]];
self.recallSafePan.y = 768 - 141;
[self.recallSafePan setUserInteractionEnabled:YES];
self.channelNumber = channel;
[self setClipsToBounds:NO];
_background = [[UIView alloc] initWithFrame:CGRectMake(0, 80, 122, 547)];
[_background setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"singleFader"]]];
[self addSubview:_background];
[self addChannelName];
[self addInput];
[self addPan];
[self addOn];
[self addMeter];
[self addFader];
}
So the issue im having is, i need to call this func to setup my custom cell to look and function how i need it to. However, every time i call reload on the collectionview due to state changes, it layers the new views over the top of the old views (due to reocurring imageview alloc's)
So how can i apply this func to each cell automatically, yet not reapply it over the top every time i reload the cell?
Image of the issue ina view debugger here:
UICollectionViewCells are reused. So, avoid doing addSubView: (directly or indirectly as in your case: collectionView:cellForRowAtIndexPath: is calling initData: which is calling addSubView:), each time in collectionView:cellForItemAtIndexPath:.
You are using a UICollectionView and UICollectionViewCell only by code.
So there is no XIB/Storyboard were is the cell, and no IBOutlet.
According to the doc of dequeueReusableCellWithReuseIdentifier:forIndexPath:, that's the part you fall into:
If you registered a class for the specified identifier and a new cell
must be created, this method initializes the cell by calling its
initWithFrame: method.
So, in CDCChannelStrip.m, override initWithFrame: and initialize its properties (that are "always" visible/used).
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
_myImageViewProperty = [[UIImageView alloc] initWithFrame:someFrame];
[self addSubview:_myImageViewProperty];
etc.
}
return self;
}
-(void)updateWithChannel:(NSInteger)channel
{
self.channelNumber = channel;
self.channelLabelName = #"Something";
//etc.
}
You can also use prepareForReuse to "reinit" the cell as "virgin" (like it was just after initWithFrame: for instance.
-(void)prepareForReuse
{
[super prepareForReuse];
self.channelLabelName = #"";
self.backgroundColor = [UIColor clearColor];
//etc.
[self.sometimesThatImageIsShown setHidden:YES];
}
Simplest way is to create a separate xib for cell or prototype cell. Design you cell there and in cellForItemAtIndexPath you just manipulate your views and set values to your labels, images and other controllers.
Related
I have created a UICollectionView programmatically. I use a custom UICollectionViewCell subclass. Inside the cell class, I create a label with a class of my own (easier and quicker to set up its appearance).
The problem I'm having is : for several cells, the collectionView does not layout the label content. I know that the data is here (print in the console) that is, the text property of the cell does contain the string data I want to show, but for some reason the collectionView does not display the label content.
I tried with a simple test (print 'toto' inside the label) and I get a few toto's here and there, but not in all of the cells. As you can see I have 2 UICollectionViews inside the same ViewController, and this is why I test whether it's one or the other in the DataSource implementation.
Please tell me if you need more code.
Here's the code :
-(void)createBottomCollectionView {
// Layout
UICollectionViewFlowLayout *collectionViewLayout = [[UICollectionViewFlowLayout alloc] init];
collectionViewLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
collectionViewLayout.minimumLineSpacing = 0.0;
// UICollectionView
self.bottomCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(20, 354+20, 320-2*20, 35) collectionViewLayout:collectionViewLayout];
self.bottomCollectionView.showsHorizontalScrollIndicator = NO;
self.bottomCollectionView.bounces = YES;
self.bottomCollectionView.alwaysBounceHorizontal = YES;
self.bottomCollectionView.alwaysBounceVertical = NO;
self.bottomCollectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.bottomCollectionView.dataSource = self;
self.bottomCollectionView.delegate = self;
[self.bottomCollectionView registerClass:[SetFormatCollectionViewCell class] forCellWithReuseIdentifier:SetFormatCollectionViewCellIdentifier];
// Background
self.bottomCollectionView.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.bottomCollectionView];
[self.bottomCollectionView reloadData];
}
CollectionView datasource (dumb test with a "toto" value) in the real app I pull data with CoreData
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
if (collectionView == self.bottomCollectionView) {
SetFormatCollectionViewCell *cell = (SetFormatCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:SetFormatCollectionViewCellIdentifier forIndexPath:indexPath];
cell.text = #"toto";
return cell;
}
if (collectionView == self.collectionView) {
TrackingSetCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:TrackingSetCollectionViewCellIdentifier forIndexPath:indexPath];
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
return nil;
}
Custom Cell class :
#interface SetFormatCollectionViewCell : UICollectionViewCell
#property (strong,nonatomic) NSString *text;
#end
#implementation SetFormatCollectionViewCell
{
FormatLabel *aFormatLabel;
}
-(id)initWithFrame:(CGRect)frame {
if (self=[super initWithFrame:frame]) {
// Initialization code
aFormatLabel = [[FormatLabel alloc]initWithFrame:self.frame textColor:[UIColor blackColor] font:[UIFont fontWithName:#"ITCAvantGardeStd-Bk" size:22] alpha:1.0f border:YES];
[self.contentView addSubview:aFormatLabel];
}
return self;
}
-(void)prepareForReuse {
[super prepareForReuse];
self.text = #"";
}
-(void)setText:(NSString *)text {
_text = [text copy];
aFormatLabel.text = self.text;
}
FormatLabel Class (not important I think)
#interface FormatLabel ()
#property (assign,nonatomic) UIEdgeInsets edgeInsets;
#end
#implementation FormatLabel
-(id)initWithFrame:(CGRect)frame textColor:(UIColor *)color font:(UIFont *)font alpha:(CGFloat)alphaValue border:(BOOL)withBorder{
self = [super initWithFrame:frame];
if (self) {
// Set up
self.textAlignment = NSTextAlignmentCenter;
self.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
self.adjustsFontSizeToFitWidth = YES;
self.textColor = color;
self.alpha = alphaValue;
self.font = font;
if (withBorder) {
self.layer.borderWidth = 1.0f;
self.layer.borderColor = color.CGColor;
}
self.edgeInsets = UIEdgeInsetsMake(9, 6, 8, 6);
}
return self;
}
Thanks for helping
EDIT: for those who might have the same issue, I post 3 shots of the problem (you can find the answer just below). The 2nd shot contains colored cell the see the problem. The third shot is the one that I took just after accepting jmkk's answer.
Thanks for all the other answers!
Your problem lies with positioning of the FormatLabel view within the cell.
You're using cell's frame as the frame for the label, while what you need is the cell's bounds.
Cell's frame is relative to its superview, so applying the same position to cell's subview renders it offset relative to the cell itself.
Fix your code to do this:
aFormatLabel = [[FormatLabel alloc]initWithFrame:self.bounds textColor:[UIColor blackColor] font:[UIFont fontWithName:#"ITCAvantGardeStd-Bk" size:22] alpha:1.0f border:YES];
I have written a subclass of UITableViewCell to allow horizontal swipe to give some actions to users. Here is what I am doing:
Create a scrollView
Create a buttonView and add in scrollView.
Create a UIButton and add all cell controls as subview to it. Add in scroll view.
Add scrollView to cell contentView.
For #3 I am setting the highlighted image to give a feel of user tap like in normal cell.
The issue is when my table view is loaded on iOS 6 with 6 cells and user tap on any of the cell, cell gets highlighted properly and the details are shown properly for the tapped cell. But if user scrolls up and first cell is re-used and user tap on the top cell (which is second row), cell next to it gets highlighted. If user scrolls up and purge 2 cells and tap on the top cell, cell 2 cells down it gets highlighted. Although tapped cell shows the data of the correct cell.
Any clue?
- (id)initWithStyle:(UITableViewCellStyle)iStyle reuseIdentifier:(NSString *)iReuseIdentifier andMenuButtonDetails:(NSArray *)iMenuButtonDetails {
if ((self = [super initWithStyle:iStyle reuseIdentifier:iReuseIdentifier])) {
self.catchWidth = kMenuButtonWidth * [iMenuButtonDetails count];
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(kScreenOrigin, kScreenOrigin, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds))];
self.scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.bounds) + self.catchWidth, CGRectGetHeight(self.bounds));
self.scrollView.delegate = self;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.scrollEnabled = YES;
[self.contentView addSubview:self.scrollView];
self.scrollViewButtonView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.bounds) - self.catchWidth, kScreenOrigin, self.catchWidth, CGRectGetHeight(self.bounds))];
[self.scrollView addSubview:self.scrollViewButtonView];
if ([iMenuButtonDetails count]) {
// Adding menu buttons to the cell.
CGFloat anXOffset = kScreenOrigin;
for (NSDictionary *aMenuButton in iMenuButtonDetails) {
if ([aMenuButton containsObjectForKey:kTitleKey]) {
UIButton *aButton = [[UIButton alloc] initWithFrame:CGRectMake(anXOffset, kScreenOrigin, kMenuButtonWidth, kCellHeight64)];
[aButton addTarget:self action:#selector(buttonSelected:) forControlEvents:UIControlEventTouchUpInside];
if ([aMenuButton containsObjectForKey:kButtonTagKey])
aButton.tag = [[aMenuButton stringForKey:kButtonTagKey] intValue];
aButton.titleEdgeInsets = UIEdgeInsetsMake(kScreenOrigin, 2.0, kScreenOrigin, 2.0);
aButton.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
[aButton.titleLabel setTextAlignment:NSTextAlignmentCenter];
[aButton setTitle:[aMenuButton stringForKey:kTitleKey] forState:UIControlStateNormal];
[aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
if ([aMenuButton objectForKey:kButtonColorKey]) {
aButton.backgroundColor = [aMenuButton objectForKey:kButtonColorKey];
}
[self.scrollViewButtonView addSubview:aButton];
anXOffset += kMenuButtonWidth;
}
}
}
self.scrollViewContentView = [UIButton buttonWithType:UIButtonTypeCustom];
self.scrollViewContentView.frame = CGRectMake(kScreenOrigin, kScreenOrigin, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds));
if (![Utilities isIOS7orAbove]) {
[self.scrollViewContentView addTarget:self action:#selector(cellHighlighted) forControlEvents:UIControlEventTouchDown];
[self.scrollViewContentView addTarget:self action:#selector(cellCancelHighlight) forControlEvents:UIControlEventTouchDragExit];
}
[self.scrollViewContentView addTarget:self action:#selector(selectCell:) forControlEvents:UIControlEventTouchUpInside];
self.scrollViewContentView.backgroundColor = [UIColor whiteColor];
UIImage *aBGHighlightedImage = nil;
if ([Utilities isIOS7orAbove]) {
aBGHighlightedImage = [UIImage imageNamed:kCellHighlightedImageIOS7];
} else {
aBGHighlightedImage = [UIImage imageNamed:kCellHighlightedImageIOS6];
}
[self.scrollViewContentView setBackgroundImage:[aBGHighlightedImage stretchableImageWithLeftCapWidth:11.0f topCapHeight:0] forState:UIControlStateHighlighted];
[self.scrollView addSubview:self.scrollViewContentView];
[self.scrollViewContentView addSubview:self.imageView];
[self.scrollViewContentView addSubview:self.textLabel];
[self.scrollViewContentView addSubview:self.detailTextLabel];
}
- (void)prepareForReuse {
[super prepareForReuse];
self.scrollViewContentView.enabled = YES;
[self.scrollView setContentOffset:CGPointZero animated:NO];
}
- (UITableViewCell *)tableView:(UITableView *)iTableView cellForRowAtIndexPath:(NSIndexPath *)iIndexPath {
MyTableViewCell *aCell = (MyTableViewCell *)[iTableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
if (!aCell) {
aCell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"CellIdentifier" andMenuButtonDetails:aMenuButtons];
}
// Set data on cell now
return aCell
}
Let me know if there is something I'm missing here, but it seems like you're adding a ton of complexity to your class for no reason. Are you familiar with UICollectionView?
Here's an example implementation (which scrolls horizontally):
#interface asdf () <UICollectionViewDataSource, UICollectionViewDelegate>
#property (strong, nonatomic) UICollectionView *collectionView;
#end
#implementation asdf
- (id)init
{
self = [super init];
if (self)
{
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.collectionViewLayout];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.view addSubview:self.collectionView];
NSString *className = NSStringFromClass([UICollectionViewCell class]);
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:className];
}
return self;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.collectionView.frame = self.view.bounds;
}
- (UICollectionViewLayout *)collectionViewLayout
{
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.minimumInteritemSpacing = 0;
layout.minimumLineSpacing = 0;
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
return layout;
}
#pragma mark - UICollectionView
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 5;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSString *className = NSStringFromClass([UICollectionViewCell class]);
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:className forIndexPath:indexPath];
// This is for dequeuing
NSInteger tag = 12324;
UIView *view = [cell viewWithTag:tag];
if (view)
[view removeFromSuperview];
view = [[UIView alloc] initWithFrame:cell.bounds];
view.tag = tag;
// Add all of your subviews to the view property
[cell addSubview:view];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(collectionView.bounds.size.width, 50);
}
#end
I wrote this quickly as a sample, so it's not tailored specifically to what you're building, but this should give you a nice idea of how simple it is to implement a UICollectionView.
This answer may come across as random for what you're asking, but when possible, you should always try to use what Apple provides over what you would spend precious hours re-inventing the wheel w/ & likely experience random nuances like yours.
I'm working on a project similar to a video album. In that I'm using UICollectionView to display the thumb images of those videos. The worst part is that I should not use storyboard or xib files. I have tried to do this programatically. I'm currently working on this code:
- (void)viewDidLoad
{
i = 0;
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
[layout setItemSize:CGSizeMake(self.view.frame.size.width/2.5,self.view.frame.size.width/2.5)];
collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
[collectionView setDataSource:self];
[collectionView setDelegate:self];
collectionView.backgroundColor = [UIColor whiteColor];
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"MyCell"];
[self.view addSubview:collectionView];
[super viewDidLoad];
}
I have given 1 for return in numberOfSectionsInCollectionView and [myArray count] for return in numberOfItemsInSection.
-(UICollectionViewCell *) collectionView:(UICollectionView *)cV cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [cV dequeueReusableCellWithReuseIdentifier:#"MyCell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor blackColor];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(cell.frame.origin.x , cell.frame.origin.y, cell.frame.size.width, (cell.frame.size.height - cell.frame.size.height/3))];
cell.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(cell.frame.origin.x , cell.frame.origin.y, cell.frame.size.width, (cell.frame.size.height - cell.frame.size.height/3))];
imageView.image = [UIImage imageNamed:[storeData objectAtIndex:i]];
[cell addSubview:imageView];
i++;
return cell;
}
I have rechecked the images in myArray. When the view loads, the collection view shows only the first image. Other 4 cells are empty. What is wrong with my code?
For those who is experiencing this problem, frame is the key. I've encountered this and changed:
cell.imageView.frame = cell.frame;
into
cell.imageView.frame = cell.bounds;
The op is using:
cell.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(cell.frame.origin.x , cell.frame.origin.y, cell.frame.size.width, (cell.frame.size.height - cell.frame.size.height/3))];
That's why this happened.
You shouldn't be using i as a counter. The whole point of the delegate method sending you an indexPath is that it tells you what information to get from your array of source data. So, remove i and use the indexPath.row instead.
You also don't need 2 image views. But you should probably keep your special subview and not use the cells built in image view.
You do not need a counter. As indicated by Wain, use indexPath.row.
Importantly, you should not create new subviews in cellForItemAtIndexPath, but rather use this method to fill them appropriately with content. You could put the image views into your storyboard prototype cells and identify them with tags. Each cell returned from dequeueReusableCellWithReuseIdentifier will already contain the image view.
you should never create ui elements in cellForRowAtIndexPath:. Subclass a collection view cell like so:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSLog(#"INIT WITH FRAME FOR CELL");
//we create the UIImageView here
imageView = [[UIImageView alloc] init];
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.frame = CGRectMake(cell.frame.origin.x , cell.frame.origin.y, cell.frame.size.width, (cell.frame.size.height - cell.frame.size.height/3));
[self.contentView addSubview:imageView]; //the only place we want to do this addSubview: is here!
}
return self;
}
Then add that subclassed cell as a property and alter this code:]
[collectionView registerClass:[customCellClass class] forCellWithReuseIdentifier:#"MyCell"];
The perform these changes:
-(customCellClass *) collectionView:(UICollectionView *)cV cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = (customCellClass *)[cV dequeueReusableCellWithReuseIdentifier:#"MyCell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor blackColor];
imageView.image = [UIImage imageNamed:[storeData objectAtIndex:indexPath.row]];
return cell;
}
A final adjustment would be to move the the [super viewDidLoad] to this:
- (void)viewDidLoad
{
[super viewDidLoad];
//insert the rest of the code here rather than before viewDidLoad
}
I'm trying to display an image in only certain cells in my UITableView. Here's a my configureCell method:
-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
StoryInfo *info = [self.fetchedResultsController objectAtIndexPath:indexPath];
UIImage *ribbon = [UIImage imageNamed:#"ribbon.png"];
UIImageView *ribbonView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
[ribbonView setImage:ribbon];
[cell addSubview:ribbonView];
if([[NSNumber numberWithBool:NO] isEqualToNumber:info.visited]) {
cell.textLabel.textColor = [UIColor colorWithRed:53/255.0 green:53/255.0 blue:52/255.0 alpha:1];
ribbonView.hidden = NO;
}
else {
cell.textLabel.textColor = [UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0];
ribbonView.hidden = YES;
}
}
And here's cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// set up the cell...
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
This doesn't quite work because at first, all of the cells draw them ribbonView, regardless of the value of info.visited. I've stepped through the if/else and I see that the hiding code it being hit, though. But, if I navigate away from the list, and then come back, the correct ribbon state is visible. Scrolling the table view breaks it again.
The font colors are always correct, though.
If you are reusing cells, then it could likely be that you are adding multiple ribbonView subviews to the cell, so even if the info.visited for the current indexPath is NO, there is another ribbonView leftover on the cell that you can still see.
The thing to do is make certain you only ever have one ribbonView subview, which can be done either by removing old ribbonViews in your configuration method, or better by subclassing UITableViewCell and adding a ribbonView property to the cell, which gets set once and added to the cell's view hierarchy once, and which you can then access and set hidden to NO or YES in the configuration method.
EDIT: The cell text color will always be correct since you are changing the color on one instance of UILabel that is in the cell's view hierarchy. I expect you'd see the same buggy behavior if instead your configuration method added a new UILabel subview to the cell each time it was configured.
EDIT: Code to try
-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
static NSInteger ribbonTag = 12345;
StoryInfo *info = [self.fetchedResultsController objectAtIndexPath:indexPath];
// re-use a ribbonView if one's already been added to this cell
UIImageView *ribbonView = [cell.contentView viewWithTag:ribbonTag];
if (!ribbonView){
ribbonView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
ribbonView.tag = ribbonTag;
UIImage *ribbon = [UIImage imageNamed:#"ribbon.png"];
[ribbonView setImage:ribbon];
// add subviews to contentView
[cell.contentView addSubview:ribbonView];
}
if([[NSNumber numberWithBool:NO] isEqualToNumber:info.visited]) {
cell.textLabel.textColor = [UIColor colorWithRed:53/255.0 green:53/255.0 blue:52/255.0 alpha:1];
ribbonView.hidden = NO;
}
else {
cell.textLabel.textColor = [UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0];
ribbonView.hidden = YES;
}
}
If you are using dequeueReusableCellWithIdentifier:forIndexPath: in the tableView:cellForRowAtIndexPath: method. Every time you reutilize a cell, you will create a new UIImageView and put it over the last one.
But to solve this you dont need to subclass. Not yet, because your cells are simple still. If you want to add more subviews, then subclassing is the only option.
One solution I can think of would be this:
-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
StoryInfo *info = [self.fetchedResultsController objectAtIndexPath:indexPath];
UIImageView *ribbonView = nil;
//My code:
for ( UIView *childView in cell.subviews ) {
if([childView isKindOfClass:[UIImageView class]] {
ribbonView = childView;
break;
}
}
//Note: this doesnt work if you have more than one UIImageView in your cell.
if(ribbonView == nil) {
UIImage *ribbon = [UIImage imageNamed:#"ribbon.png"];
ribbonView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
[ribbonView setImage:ribbon];
[cell addSubview:ribbonView];
}
//Ends here.
if([[NSNumber numberWithBool:NO] isEqualToNumber:info.visited]) {
cell.textLabel.textColor = [UIColor colorWithRed:53/255.0 green:53/255.0 blue:52/255.0 alpha:1];
ribbonView.hidden = NO;
}
else {
cell.textLabel.textColor = [UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0];
ribbonView.hidden = YES;
}
}
Try it and tell me if it works.
Good luck.
That is definitely an issue in implementation of
tableView:cellForRowAtIndexPath:
You need to always call
dequeueReusableCellWithIdentifier:forIndexPath:
and then call your configure method
configureCell:atIndexPath:(NSIndexPath *)indexPath
EDIT:
Also, I think you also need to do [cell.contentView addSubView:...] instead of [cell addSubView:...]
I have a collection cell and want to change the image when touched, and back again, how should I structure it?
After highlighting (works well below), I want it to go back to the old image when touched again. Thank you. At viewWillDissapear I want to know which cells are highlighted.
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
UIImage *backGround =[UIImage imageNamed:#"IconHighlight.png"];
UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, backGround.size.width, backGround.size.height)];
av.backgroundColor = [UIColor clearColor];
av.opaque = NO;
av.image = backGround;
[[collectionView cellForItemAtIndexPath:indexPath] setBackgroundView:av];
[[collectionView cellForItemAtIndexPath:indexPath].backgroundView setTag:1];
}
Create CustomCell - subclass of UICollectionViewCell. Customize init to the following
//CustomCell.m
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
backgroundImageView.image = [UIImage imageNamed:#"background.png"];
highlightImageView.image = [UIImage imageNamed:#"highlight.png"];
self.backgroundView = backgroundImageView;
_isHighlight = -1;
}
return self;
}
-(void)tapToChangeBackGround{
self.isHighlight = -self.isHighlight;
if (self.isHighlight==1) {
self.backgroundView = highlightImageView;
}
else{
self.backgroundView = backgroundImageView;
}
}
//didSelect
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
CustomCell *cell = (CustomCell *)[collectionView cellForItemAtIndexPath:indexPath];
[cell tapToChangeBackGround];
}