cell.selectedbackgroundview isn't working. I have figured out that the selectedview is placed behind my image in the cell resulting in no visible image (I worked this out by making the image small then a saw the tick behind the image). Is there a way to fix this? My code seems fine it must have something to do with the layers
static NSString *identifier = #"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *productImages = (UIImageView *)[cell viewWithTag:100];
productImages.image = [UIImage imageNamed:[AllProducts_Sections[indexPath.section]objectAtIndex:indexPath.row]];
collectionView.backgroundColor = [UIColor greenColor];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"tick 1.png"]];
collectionView.allowsMultipleSelection = YES;
return cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellIdentifier = #"Cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
//some code
}
UIView *selectedBackView=[[UIView alloc]initWithFrame:cell.bounds];
UIImageView *selectionImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"tick 1.png"]];
[selectedBackView addSubview:selectionImg];
cell.selectedBackgroundView=selectedBackView;
return cell;
}
Also make sure that tick 1.png is in your project's bundle.
Related
I am following a tutorial that uses a custom cell created in Storyboard in a tableview and then adds an accessory view. The table is loading okay. However when I try to add an accessory view (an image), nothing appears. Could there be some setting in storyboard that is wrong? A problem in the code? Or why is the accessory view not appearing
Here is the method where the tutorial says to add the accessory view:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
IDContactImport *contactImport = self.contacts[indexPath.row];
IDTableViewCell *idTableCell =(IDTableViewCell *)cell;
idTableCell.contactImport=contactImport;
if (contactImport.imageData==nil)
{
idTableCell.iconView.image = [UIImage imageNamed:#"headshot.jpg"];
}
else{
idTableCell.iconView.image = [UIImage imageWithData:contactImport.imageData];
}
//HERE IS ACCESSORY VIEW
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"checkbox.jpg"]];
idTableCell.accessoryView = imageView;
// [self updateAccessoryForTableCell:cell atIndexPath:indexPath];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
return cell;
}
Thanks for any suggestions
static NSString *cellIdentifier = #"CELLIDENTIFIER";
UITableViewCell *customCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
UIImageView *sampleImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"sampleImage.png"]];
[customCell setAccessoryView:sampleImage];
Hope it useful for you.!!!
It should work. Please try blow code. there is no issue on Storyboard.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"customCell";
CustomCellView *cell = [ctableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
long row = [indexPath row];
cell.name.text = carname[row];
cell.model.text = carModel[row];
cell.carImg.image = [UIImage imageNamed:carImage[row]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"SelectedStar_green.png"]];
cell.accessoryView = imageView;
// Configure the cell...
return cell;
}
Change the dequeueReusableCellWithIdentifier: call to:
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
This will always returns a cell, so you will never have to check for nil.
// if (cell == nil) {
// cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
// }
I don't think any of the above answers have addressed your issue. You are right by mentioning that it all has to do with your storyboard.
In order to fix this, go into your Main.storyboard, click on the table view and reset the view to suggested constraints following the below picture.
I have UITableView with cells that have images on it. Im using storyboard, and connect UIImageView as outlet and name it imageView. Images what I get come with different size (i download it through URL). But when table shows up sometimes image shown in incorrect size (it suppose to be square with 66x66 width and height, but in many cases square is bigger, or it actually have more width then height).
Obviously i want that borders of my UIImageView be stable. There is my code for cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UILabel *labelText = (UILabel *)[cell viewWithTag:1000];
labelText.text = [[self.listOfPlaceDetails objectAtIndex:indexPath.row] objectForKey:#"name"];
if (cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[cell.imageView setImageWithURL:[NSURL URLWithString:[[self.listOfPlaceDetails objectAtIndex:indexPath.row]objectForKey:#"imageFirst"]] placeholderImage:[UIImage imageNamed:#"dashie.png" ]completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType){
UIImage *resizedImage = [image resizedImageWithBounds:CGSizeMake(66, 66)];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.imageView.layer.cornerRadius = cell.imageView.bounds.size.width /2.0f;
cell.imageView.clipsToBounds = YES;
cell.separatorInset = UIEdgeInsetsMake(0, 82, 0, 0);
cell.imageView.image = resizedImage ;
}];
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
return cell;
}
Please explain me whats going on and how to fix that, any advice would be appreciated, thanks!
Add new imageView and set the image. I would recommend you make subclass of uitablviewCell and add the imageView as outlet view
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UILabel *labelText = (UILabel *)[cell viewWithTag:1000];
labelText.text = [[self.listOfPlaceDetails objectAtIndex:indexPath.row] objectForKey:#"name"];
if (cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:yourFrame];
imgView.contentMode = UIViewContentModeScaleAspectFit;
[cell addSubview:imgView];
imgView.tag = 7;
}
UIImageView *img = (UIImageView*)[cell viewWithTag:7];
UIImage *resizedImage = [image resizedImageWithBounds:CGSizeMake(66, 66)];
img.imqge = resizedImage
You are using standard UITableViewCell class and try to change its imageView property. Not your cell, that you created on Storyboard.
You should look through this tutorial Storyboards Tutorial in iOS 7: Part 1.
Your are interested in part:
Designing Your Own Prototype Cells
Using a Subclass for the Cell
I'll recommend use subclass of UITableViewCell issue.
I have a little problem. I want to display multiple (amount is dynamic) UIImages in a UITableViewCell (in the center).
How can I do that (UIImages are generated programmatically)?
Thanks a lot!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"S%1dR%1d",indexPath.section,indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
self.imgThings = [[UIImageView alloc] init];
// imgThings.backgroundColor= [UIColor magentaColor];
[self.imgThings setImage: [UIImage imageNamed:#"image.png"]];
self.imgThings.center = CGPointMake(cell.contentView.bounds.size.width/2,cell.contentView.bounds.size.height/2);
[cell.contentView addSubview:self.imgThings];
}
return cell;
}
Above is simple example for only one image, u can also set it for multiple image by changing name of image.
I'm having a bit of difficulty understanding what is going wrong and how to fix this.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
NSUInteger row = [indexPath row];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
UILabel *where = [[UILabel alloc] initWithFrame:CGRectMake(88.0, 0.0, 155.0, 22.0)];
where.text = [delegate.destinationArray1 objectAtIndex:row];
where.font = [UIFont fontWithName:#"Helvetica" size:12.0];
where.textColor = [UIColor blueColor];
[cell.contentView addSubview:where];
return cell;
}
This doesn't work properly but this does:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSUInteger row = [indexPath row];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
UILabel *where = [[UILabel alloc] initWithFrame:CGRectMake(88.0, 0.0, 155.0, 22.0)];
where.text = [delegate.destinationArray1 objectAtIndex:row];
where.font = [UIFont fontWithName:#"Helvetica" size:12.0];
where.textColor = [UIColor blueColor];
[cell.contentView addSubview:where];
return cell;
}
They both get populated by "delegate.destinationArray1" but when all the code is inside the curly braces of
if(cell == nil)
the list gets unordered and repeats cells and misses some out. I can't use the latter way as it creates a MASSIVE memory leak when scrolling.
Any ideas?
I did the exact same thing when I started using UITableViews. The reason for the memory leak is that in the second implementation (the one that works) you are actually creating every cell, every single time. Let me try to explain a bit more.
You never want to set content of a cell between the (cell == nil). The reason for this is the reuseIdentifier. If the table needs to display a new cell it will grab one and see if it has already been alloced/inited. If it has it will just use it. If that is the case the content will already be set in the cell you grabbed and you are not telling it to set it any differently.
between the (cell == nil) only create and establish the view. Not the content. All content should be set after. So then no matter what cell it grabs it can always set the content. So this is what you want:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell) // or (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
UILabel *where = [[UILabel alloc] initWithFrame:CGRectMake(88.0, 0.0, 155.0, 22.0)];
where.font = [UIFont fontWithName:#"Helvetica" size:12.0];
where.textColor = [UIColor blueColor];
where.tag = 1;
[cell addSubview:where];
}
NSUInteger row = indexPath.row;
UILabel *where = [cell viewWithTag:1];
where.text = [delegate.destinationArray1 objectAtIndex:row];
return cell;
}
I just coded this in StackoverFlow so sorry if there are any small syntax errors.
The cell object is reused or created by the first statement. After checking cell for nil and creating a cell, you must not create another cell.
So delete the line
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
which comes after
NSUInteger row = [indexPath row];
and you'll work with the correct cell object.
When the tableview reuses cells it is based on the CellIdentifier. The tableview doesn't care what attributes you've set on the cell. In the first case the reuse it happening and it recognizes a cell it can use but that cell has the wrong information on it.
What I do is subclass UITableViewCell and do all the work inside of that class. Here is a quick snippet
#implementation AlertCell
//Custom init method
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier withHeight:(float)height {
//Whatever you need to do
}
//Place the views
- (void)layoutSubviews {
}
//Custom Setter method
- (void)setAlert:(CWAlert *)incomingAlert withAssets:(NSDictionary *)assets {
}
#end
Then you do something like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier;
CWAlertCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[AlertCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier withHeight:[self convertAssetsLengthToCellHeight:assetsLength]];
UIView *selectedView = [[UIView alloc] init];
selectedView.backgroundColor = [UIColor colorWithHexString:#"F6F6F6"];
cell.selectedBackgroundView = selectedView;
}
NSDictionary *alertInfo = [AlertCell getNeededCellAssets:alert];
[cell setAlert:alert withAssets:alertInfo];
return cell;
}
I can show more code from the subclass if needed.
I have a UITableView which a UIImage is added as a subview to each cell of it. The images are PNG transparent. The problem is when I scroll through the UITableView, the images get overlapped and then I receive the memory warning and stuff.
here's the current code for configuring a cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
int cellNumber = indexPath.row + 1;
NSString *cellImage1 = [NSString stringWithFormat:#"c%i.png", cellNumber];
UIImage *theImage = [UIImage imageNamed:cellImage1];
UIImageView *cellImage = [[UIImageView alloc] initWithImage:theImage];
[cell.contentView addSubview:cellImage];
return cell;
}
I know the code for removing the UIImage subview would be like:
[cell.imageView removeFromSuperview];
But I don't know where to put it. I've placed it between all the lines; even added an else, in the if statement. didn't seem to work!
UITableViewCell already have a image view attached. Remove:
UIImageView *cellImage = [[UIImageView alloc] initWithImage:theImage];
[cell.contentView addSubview:cellImage];
Add:
[cell.imageview setImage:theImage];
and you're good to go!
Result code will be like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UIImage *cellImage = [UIImage imageNamed:[NSString stringWithFormat:#"c%i.png", indexPath.row + 1]];
[cell.imageView setImage:cellImage];
return cell;
}
You just kepp this:
[cell.imageview setImage:theImage];
and remove:
UIImageView *cellImage = [[UIImageView alloc] initWithImage:theImage];
[cell.contentView addSubview:cellImage];