iOS - when to remove subviews from UITableViewCell - uitableview

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];

Related

ios/xcode:How to Add Accessory View to Custom Cell

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.

Cell.selectedBackgroundview not showing

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.

UIImageView changing size unexpectedly

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.

custom tableview cells with different background images for ios application

I want to design a tableview such that each cell has a different background image depending on a condition. There are 10 images for cell background and I want to load them as such that the image pattern is repeated on the cells. For example, cells 1-10 take images from 1-10 respectively. Then cells 11-20 also take images 1-10 and so on. How can I achieve this?
give image name like: images_1/2.../10.png
//write it
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell1";
CustomCell *Cell = (CustomCell *)[tabeleYourTurn dequeueReusableCellWithIdentifier:CellIdentifier];
if (Cell == nil)
{
NSArray *topLevelObject= [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
}
for (id currentobject in topLevelObject)
{
if ([currentobject isKindOfClass:[UITableViewCell class]])
{
Cell = (CustomCell *) currentobject;
break;
}
}
}
NSString *image = [NSString stringWithFormat:#"images_%d.png", indexPath.row % 10 + 1];
Cell.imageView.image = [UIImage imageNamed:image];
return Cell;
}
May be it will work.
First create an array with ImageName..
Like
NSArray *ImageArray=[[NSArray alloc]initWithObjects:#"1.png",#"2.png"];
Then this array with 10 images
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// create a background image for the cell:
UIImageView *bgView = [[UIImageView alloc] initWithFrame:CGRectZero];
[cell setBackgroundColor:[UIColor clearColor]];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell setBackgroundView:bgView];
[cell setIndentationWidth:0.0];
((UIImageView *)cell.backgroundView).image = [UIImage imageNamed:[ImageArray objectAtIndex:indexPath.row%10]];
}
Assuming that your image names are 1, 2...10, you can try to do something like this:
NSString *imageName = [NSString stringWithFormat:#"%d", indexPath.row % 10 + 1];
Than you can use this imageName where you want.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if( (indexPath.row % 10 ) <= 1)
{
// set your image
cell.imageView.image = [UIImage imageNamed:#"image1.png"];
}
}

IOS centered UIImages in a row

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.

Resources