Resizing images on TableView - ios

- (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];
}
// Set the data for this cell:
cell.textLabel.text = [namesArray objectAtIndex:indexPath.row];
cell.textLabel.textColor = [UIColor grayColor];
cell.textLabel.font = [UIFont fontWithName:#"Trebuchet MS" size:14];
cell.textLabel.textAlignment = UITextAlignmentLeft;
cell.imageView.image = [UIImage imageNamed:[imagesArray objectAtIndex:indexPath.row ]];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 5, 5)];
imgView.image = [UIImage imageNamed:[imagesArray objectAtIndex:indexPath.row ]];
cell.imageView.image = imgView.image;
[cell.imageView setFrame:CGRectMake(0, 0, 55, 55)];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
return cell;
}
Here I'm using just TableView not UITableViewCell, but I want to resize images on TableView. I've taken a look at many examples on the internet, however I don't understand how any of them work - I'm not able to get them resized on TableView. Could someone explain how to get it working? [cell.imageView setFrame:CGRectMake(0, 0, 55, 55)]; is not working

Here i put logic of both Crop and resize image, use it as per your requirement.
For Get Cropped Image:
UIImage *croppedImg = nil;
CGRect cropRect = CGRectMake(AS YOu Need); // set frame as you need
croppedImg = [self croppIngimageByImageName:self.imageView.image toRect:cropRect];
The following method that return UIImage (as You want size of image)
- (UIImage *)croppIngimageByImageName:(UIImage *)imageToCrop toRect:(CGRect)rect
{
//CGRect CropRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height+15);
CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
UIImage *cropped = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return cropped;
}
Here you get Croped Image that return by above method;
OR RESIZING
And also use following method with specific hight and width of image for Resizing:
+ (UIImage*)resizeImage:(UIImage*)image withWidth:(CGFloat)width withHeight:(CGFloat)height
{
CGSize newSize = CGSizeMake(width, height);
CGFloat widthRatio = newSize.width/image.size.width;
CGFloat heightRatio = newSize.height/image.size.height;
if(widthRatio > heightRatio)
{
newSize=CGSizeMake(image.size.width*heightRatio,image.size.height*heightRatio);
}
else
{
newSize=CGSizeMake(image.size.width*widthRatio,image.size.height*widthRatio);
}
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
This method return NewImage, with specific size that you want.

If you want to customize the aspect of the cell (for example the size of the image view) you need to use a custom UITableViewCell. Currently you are using a standard cell UITableViewCellStyleDefault which doesn't let you customize the size of the image view.
Create an UITableViewCell in storyboard or in a Xib file, add it your image view and set its tag to a number. After that you need to retrieve the cell for configuring it:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UIImageView *imageView = (UIImageView *) [cell viewWithTag:TAG_NUMBER];
// do your stuff
return cell;
}

Maybe you can do something like this. Just add a new imageView under the cell's contentView atleast it will be easier for you to set the frame.
And just to clarify you are already using UITableviewCell.
- (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];
// Create a new imageview to have your desired frame
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 5, 5)];
// Add tag
imgView.tag = 100;
// Add the imageView to the contentView of the cell
[cell.contentView addSubview:imgView];
}
// Other codes
UIImageView *imageV = (UIImageView *)[cell.contentView viewWithTag:100];
// Assign Image here
imageV.image = [UIImage imageNamed:[imagesArray objectAtIndex:indexPath.row ]];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
return cell;
}

Related

UITableviewCell Image size changing on Click of cell?

I am trying to display name and images in UITableViewCell like the normal way
cell.textLabel.text=#"some name";
cell.imageView.image=[array objectAtIndex:indexPath.row];
and when i load the ViewController , I can see all images are coming correctly but when I tried to click on any of UITableviewCell image size is changing to big(please check the example screenshot).
Please help me to solve this problem.
Loading time it looks like
When I click on the first cell ,the cell image becomes big like below image
Please check my code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
cell.backgroundColor=[UIColor clearColor];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
cell.backgroundColor=[UIColor clearColor];
if([userName count]>0){
cell.textLabel.text=[userName objectAtIndex:indexPath.row];
[cell.imageView setImageWithURL:[NSURL URLWithString:[[userList valueForKey:#"photo"] objectAtIndex:indexPath.row]]
placeholderImage:[UIImage imageNamed:#"avatar.png"]];
}
CGSize itemSize = CGSizeMake(40, 40);
UIGraphicsBeginImageContextWithOptions(itemSize, NO, UIScreen.mainScreen.scale);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[cell.imageView.image drawInRect:imageRect];
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
cell.imageView.layer.masksToBounds=YES;
cell.imageView.layer.cornerRadius=9.0;
return cell;
}
My Didselect method is like this
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *nib;
if ([[MyDevice getMyDevice] isiPad])
{
nib = #"nibname_iPad";
}
else
{
nib = #"nibname";
}
ViewController *chat = [[ViewController alloc] initWithNibName:xibName bundle:[NSBundle mainBundle]];
chat.hidesBottomBarWhenPushed=YES;
[self.navigationController pushViewController:chat animated:YES];
}
Try to use Custom Cells (Change the Style of the cell in the attributes inspector of the cell), its better and you can design it as the way you want. Create in the Story board the UILabels and the UIImage (with a specific width and height), finally in the attributes inspector of the UILabel and UIIamge set them with a specific Tag number, this number has to be unic for both of them. And access them like this:
In the method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Insert this:
UITableViewCell *customCell = [self.tableView dequeueReusableCellWithIdentifier:#"theNameOfTheCell"];
UILabel *label;
label = (UILabel *)[customCell viewWithTag:1];
label.text = #"Some text to label with tag number 1";
label = (UILabel *)[customCell viewWithTag:2];
label.text = #"Some text to label with tag number 2";
UIImageView *imageCell;
imageCell = (UIImageView *)[customCell viewWithTag:3];
CALayer * l = [imageCell layer];
UIImage *cellImage = [UIImage imageNamed:#"theImageYouWant.png"];
imageCell.image = cellImage;
Try with this and tell what append, this is the full Documentation: Official Documentation Table View Cells
Add content mode:
[cell.imageView setContentMode:UIViewContentModeScaleAspectFit];
Hope this helps... :)

how to set background image in uitableview cell in ios7?

i am new to ios and now a days i am facing a little issue.When table view load then after scrolling up and down then background image show like this and here is my code
Actually i want to show table as chat design. i not want to use any third party.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_messages count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *comment = [_messages objectAtIndex:indexPath.row];
CGFloat whidt = 250;
UIFont *FONT = [UIFont systemFontOfSize:18];
NSAttributedString *attributedText =[[NSAttributedString alloc] initWithString:comment attributes:# { NSFontAttributeName: FONT }];
CGRect rect = [attributedText boundingRectWithSize:(CGSize){whidt, MAXFLOAT}
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
CGSize size = rect.size;
return size.height +50;
}
- (UIFont *)fontForCell{
return [UIFont boldSystemFontOfSize:18.0];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"ChatListItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSString *text = [_messages objectAtIndex:indexPath.row];
CGSize constraint = CGSizeMake(250 , 20000.0f);
UIFont *FONT = [UIFont systemFontOfSize:11];
CGSize size = [text boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSFontAttributeName:FONT }context:nil].size;
cell.textLabel.frame = CGRectMake(10, 0, 250, MAX(size.height, 54.0f) + 20.0f);
cell.textLabel.numberOfLines = 0;
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.text = text;
cell.textLabel.font = [UIFont systemFontOfSize:18];
UIImage *img = [UIImage imageNamed:#"balloon_selected_right.png"];
CGSize imgSize = cell.textLabel.frame.size;
UIGraphicsBeginImageContext( imgSize );
[img drawInRect:CGRectMake(0,0,250,imgSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
cell.textLabel.backgroundColor = [UIColor colorWithPatternImage:newImage];
/*[cell.textLabel setText:[_messages objectAtIndex:indexPath.row]];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = [_messages objectAtIndex:indexPath.row];*/
return cell;
}
Thanks is advance!
use a custom delegate method for this. here is the solution of your proble ...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Configure the cell in each row
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [self getCellContentView:CellIdentifier];
UILabel *textLabel = (UILabel *)[cell viewWithTag:1];
[textLabel setText:#""];
textLabel.text = #"some text message to show"; // your text string
return cell;
}
- (UITableViewCell *)getCellContentView:(NSString *)cellIdentifier
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
cell.backgroundColor=[UIColor clearColor];
CGRect labelRect = CGRectMake(20, 5, 100, 30); // set your desired values here
UIImage *img = [UIImage imageNamed:#"image.png"];
CGSize imgSize = labelRect.size;
UIGraphicsBeginImageContext( imgSize );
[img drawInRect:CGRectMake(0,0,250,imgSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UILabel *textLabel = [[UILabel alloc] initWithFrame:labelRect];
textLabel.tag=1;
textLabel.font=[UIFont fontWithName:#"Superclarendon" size:15];
// some of your other constraints to set ...
textLabel.backgroundColor=[UIColor colorWithPatternImage:newImage];
[cell.contentView addSubview:textLabel];
return cell;
}
Hi you can use resizableImageWithCapInsets,
UIImage *image = [[UIImage imageNamed:#"baloonImage"] resizableImageWithCapInsets:UIEdgeInsetsMake(5.0,10.0,5.0,10.0)];
[self.yourButton setBackgroundImage:image forState:UIControlStateNormal];
Modify the values (UIEdgeInsetsMake(top, left, bottom, right)) to suit any image you need.
Above code will make the image corners fixed and then after you can resize the image frame to adjust your view size,
[image setFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)];
Haseeb, based on this line of code:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]
I can see that you are not using custom table view cells.
I think the best way to solve this would be to use a custom table view cell. You would create your own class where you could design your own cell.
There's a couple of ways of doing this: 1) create a custom UITableViewCell (lots of examples on the net on how to do this), 2) you could also simply add your image as a Layer to the cell (again, lots of examples).
For example:
ImageTableViewCell : UITAbleViewCell
#property (strong, nonatomic) UIImage *backgroundImage;
Then, in your implementation something like this:
-(void)layoutSubviews
{
[super layoutSubviews];
CALayer *imageLayer = [[CALayer alloc] init];
imageLayer.frame = self.layer.bounds;
// code goes here to do whatever you want to the image ...
imageLayer.contents = self.backgroundImage.CGImage;
[self.layer addSublayer:imageLayer];
}
Food for thought, as they say ....
As suggested above use a custom prototype cell. Then in the Document Outline select the prototype cell and the Attributes inspector. Under View put Alpha to 0 Background color to Clear Color.
In swift you can set background image to UITableViewCell like below.
cell.backgroundView = UIImageView(image: UIImage(named: "yourImage.png")!)
Write this code in this function of table view.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
This image will repeat fort each row.

Animate UIImageView for Table View Cell

I am trying to animate a UIImageview in a "table view cell", but the image never shows.
Here is the code I'm trying to use.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
cell.textLabel.font = [UIFont systemFontOfSize:16.0];
}
if (cell)
{
cell.textLabel.text = [titleArray objectAtIndex:indexPath.row];
cell.textLabel.textColor = [UIColor darkGrayColor];
UIImage *musicOne = [UIImage imageNamed:#"music1.png"];
UIImage *musicTwo = [UIImage imageNamed:#"music2.png"];
UIImage *musicThree = [UIImage imageNamed:#"music3.png"];
NSArray *imagesArray = [NSArray arrayWithObjects:musicOne, musicTwo, musicThree, nil];
if (indexPath.row == 0)
{
cell.imageView.animationImages = imagesArray;
cell.imageView.animationDuration = 1.0f;
cell.imageView.animationRepeatCount = 0;
[cell.imageView startAnimating];
}
}
return cell;
}
The image shows in the cell if I don't try to do animations with several images and just use one image in the image view.
cell.imageView.image = [imagesArray objectAtIndex:0];
It is necessary to set image property of cell.imageView too along with animationImages, otherwise UITableViewCell will show nothing.
In order to improve performance of the app and to save memory do following:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:#"music1.png"];
return cell;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
UIImage *musicOne = [UIImage imageNamed:#"music1.png"];
UIImage *musicTwo = [UIImage imageNamed:#"music2.png"];
UIImage *musicThree = [UIImage imageNamed:#"music3.png"];
NSArray *imagesArray = #[musicOne, musicTwo, musicThree];
[cell.imageView setAnimationImages:imagesArray];
cell.imageView.animationDuration = 1.0f;
[cell.imageView startAnimating];
}
-(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[cell.imageView stopAnimating];
[cell.layer removeAllAnimations];
}
Try give the imageView a tag (e.g. 1) then get the image view like this:
UIImage *musicOne = [UIImage imageNamed:#"music1.png"];
UIImage *musicTwo = [UIImage imageNamed:#"music2.png"];
UIImage *musicThree = [UIImage imageNamed:#"music3.png"];
NSArray *imagesArray = [NSArray arrayWithObjects:musicOne, musicTwo, musicThree, nil];
if (indexPath.row == 0)
{
UIImageView *imageView = (UIImageView *)[cell viewWithTag:1];
imageView.animationImages = imagesArray;
imageView.animationDuration = 1.0f;
imageView.animationRepeatCount = 0;
[imageView startAnimating];
}
This exact same code worked for me. I know in UIScrollView animations will stop when view is scrolling and for that reason you should use CADisplayLink to run you animation on NSRunLoopCommonModes instead of on NSDefaultRunLoopMode, but I have tested your code and it worked.

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.

My UITableViewCell is reusing a previous cells UIImage

I have a UITable that reuses cells. Each cell has an image dependant on a condition. The correct images are always used however when cells are reused the old image is displayed behind the new one. I have tried to remove the uiimageview before adding a new subview however it has no effect.
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpTableIden = #"simpTableIden";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpTableIden];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpTableIden];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [[self.achievements objectForKey:[keys objectAtIndex:row]] objectForKey:#"name"];
CGRect picFrame = CGRectMake(cell.frame.size.width-cell.frame.size.height, 0, cell.frame.size.height, cell.frame.size.height);
UIImage *greyDot = [UIImage imageNamed:#"greyDot.png"];
UIImage *whiteDot = [UIImage imageNamed:#"whiteDot.png"];
UIImageView *achievImg = [[UIImageView alloc] init];
tableView.backgroundColor = [UIColor clearColor];
achievImg.frame=picFrame;
if ([[[self.achievements objectForKey:[keys objectAtIndex:row]] objectForKey:#"achieved"] boolValue]) {
achievImg.image = whiteDot;
}
else {
achievImg.image = greyDot;
}
[achievImg removeFromSuperview];
[cell.contentView addSubview:achievImg];
return cell;
}
Any help would be greatly appreciated?
The problem is each time cellForRowAtIndexPath: is called you create a new imageview.
I corrected your code as follows:
Here we create an imageView once for each new new cell and replace it's image every time cellForRowAtIndexPath: is called:
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpTableIden = #"simpTableIden";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpTableIden];
UIImageView *achievImg = nil;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpTableIden];
//New Cell ... Create Image view
achievImg = [[UIImageView alloc] init];
achievImg.tag = 10;
[cell.contentView addSubview:achievImg];
}
else{
//Old cell...get previously createdd imageview
achievImg = (UIImageView*)[cell.contentView viewWithTag:10];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [[self.achievements objectForKey:[keys objectAtIndex:row]] objectForKey:#"name"];
CGRect picFrame = CGRectMake(cell.frame.size.width-cell.frame.size.height, 0, cell.frame.size.height, cell.frame.size.height);
UIImage *greyDot = [UIImage imageNamed:#"greyDot.png"];
UIImage *whiteDot = [UIImage imageNamed:#"whiteDot.png"];
tableView.backgroundColor = [UIColor clearColor];
achievImg.frame=picFrame;
if ([[[self.achievements objectForKey:[keys objectAtIndex:row]] objectForKey:#"achieved"] boolValue]) {
achievImg.image = whiteDot;
}
else {
achievImg.image = greyDot;
}
//You don't need this because it doesn't have a superview in the first place..it's new
//[achievImg removeFromSuperview];
return cell;
}
try like this,
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpTableIden = #"simpTableIden";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpTableIden];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpTableIden];
NSUInteger row = [indexPath row];
cell.textLabel.text = [[self.achievements objectForKey:[keys objectAtIndex:row]] objectForKey:#"name"];
CGRect picFrame = CGRectMake(cell.frame.size.width-cell.frame.size.height, 0, cell.frame.size.height, cell.frame.size.height);
UIImage *greyDot = [UIImage imageNamed:#"greyDot.png"];
UIImage *whiteDot = [UIImage imageNamed:#"whiteDot.png"];
UIImageView *achievImg = [[UIImageView alloc] init];
tableView.backgroundColor = [UIColor clearColor];
achievImg.frame=picFrame;
if ([[[self.achievements objectForKey:[keys objectAtIndex:row]] objectForKey:#"achieved"] boolValue]) {
achievImg.image = whiteDot;
}
else {
achievImg.image = greyDot;
}
[achievImg removeFromSuperview];
[cell.contentView addSubview:achievImg];
}
return cell;
}
You are always adding the imageView to the contentView of the cell. If the cell is reused that will lead to multiple imageViews in cell.
You can give a tag to the imageView. Always check if there is a view will same tag in the contentView of cell. If not add the imageView.
UIImageView *achievImg = (UIImageView *)[cell.contentView viewWithTag:55];
if(!achievImg){
achievImg = [[UIImageView alloc] init];
achievImg.tag = 55;
[cell.contentView addSubView:achievImg];
}

Resources