Animate UIImageView for Table View Cell - ios

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.

Related

UITableView not loading data with TabBar Controller

I have tableview in one of my controller. It is not loading data into custom labels and image view with tags. I checked each and every thing, delegates are attached and I am reloading the data in viewWillAppear. I do not understand, where is the problem. I added label and images and assign them tag. Only default label of tableview is showed.
NSString *cellIdentifier;
NSMutableArray *historyArray;
#implementation CloudHistory
-(void)viewDidLoad
{
[super viewDidLoad];
historyArray = [[NSMutableArray alloc]initWithObjects:#"history1",#"history2",#"history3",#"history4",nil];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
dispatch_async(dispatch_get_main_queue(), ^{
//Reload data in services table.
[_historyTableView reloadData];
});
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//Number of section in services table.
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [historyArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
cellIdentifier = #"HistoryCell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
UILabel *name = (UILabel*)[cell viewWithTag:40];
name.text =[historyArray objectAtIndex:indexPath.row];
UIImageView *image = (UIImageView*)[cell viewWithTag:44];
image.image = [UIImage imageNamed:#"rtb_logo"];
return cell;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
//Change the background color to stoker Cloud color.
cell.selectedBackgroundView = [UIView new];
cell.selectedBackgroundView.backgroundColor = [ UIColor colorWithRed:116.0/255.0 green:174.0/255.0 blue:220.0/255.0 alpha:1.0];
}
-(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(BOOL)prefersStatusBarHidden
{
return YES;
}
#end
maybe you can export the name and image , I think some of them maybe is invalid or nil .
UILabel *name = (UILabel*)[cell viewWithTag:40];
NSLog(#"name = %#",name); // is it valid ?
UIImageView *image = (UIImageView*)[cell viewWithTag:44];
NSLog(#"image = %#", image); // is it valid ?
if some of them invalid or nil,you can do something like this:
UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, 80, 44)];
name.text = #"name";
[cell addSubview:name];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 5, 44, 44)];
imageView.image = image;
[cell addSubview:imageView];
hope it helps.
Did you create a custom UITableViewCell using a xib file?
If so, you could register it in viewdidload.
Something like this
[self.tableView registerNib:[UINib nibWithNibName:#"xibName" bundle:nil] forCellReuseIdentifier:CellIdentifier];
Then your tableview can find your custom view outlets.
Hope this helps
Why don't you use a custom UITableViewCell with your UILabel and UIImageView? So you can access the cell property, like:
cell.name.text = [historyArray objectAtIndex:indexPath.row];
cell.image = [UIImage imageNamed:#"rtb_logo"];
Anyway looks like your UILabel and UIImage is getting the values, but it's not setting them to your cell. To test you can log the value of the UILabel just to check. I don't know if it will work but have you tried this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
cellIdentifier = #"HistoryCell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
UILabel *name = (UILabel*)[cell viewWithTag:40];
name.text =[historyArray objectAtIndex:indexPath.row];
[cell viewWithTag:40] = name; // I don't know if this works
// Check it out if the name.text is getting any value
NSLog(#"UILabel %#",name.text);
UIImageView *image = (UIImageView*)[cell viewWithTag:44];
image.image = [UIImage imageNamed:#"rtb_logo"];
[cell viewWithTag:44] = image; // I don't know if this works
return cell;
}

Change Table View Cell's UIImage on Tap

Probably a simple fix.
I've got an image inside each cell of my table view controller and a tap gesture recognizer on it. That's working fine and logging appropriately.
What I need is for the image to change from its default state to then toggle between a "selected" state (green) and a "deselected" state (red). In other words, it's a checklist that's gray at first, then you tap the image, the image turns to a green checkmark, tap again, it turns to a red x.
Here's the catch: Had this working when it was only a matter of a conditional statement in the didSelectRowAtIndexPath method, but since I added the gesture recognizer and created the imageTapped method, I can't seem to translate it over. Therefore, otherwise helpful threads like this don't work for me.
Thanks as always. You guys are the best.
Here's the code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"PlacesCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"PlacesCell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
//Create ImageView
cell.imageView.image = [UIImage imageNamed:#"checkmark.png"];
//Add Gesture Recognizer
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped)];
tapped.numberOfTapsRequired = 1;
[cell.imageView addGestureRecognizer:tapped];
cell.imageView.userInteractionEnabled = YES;
[cell addSubview:cell.imageView];
return cell;
}
//Method controlling what happens when cell's UIImage is tapped
-(void)imageTapped
{
UITableViewCell *cell;
UIImage *imageDefault = [UIImage imageNamed:#"checkmark.png"];
UIImage *imageRed = [UIImage imageNamed:#"checkmark(red).png"];
UIImage *imageGreen = [UIImage imageNamed:#"checkmark(green).png"];
if (cell.imageView.image == imageDefault) {
cell.imageView.image = imageGreen;
cell.selected = true;
NSLog(#"Selected");
} else {
cell.imageView.image = imageRed;
cell.selected = false;
NSLog(#"Deselected");
}
}
i have modified your code , check it
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"PlacesCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"PlacesCell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
//Create ImageView
cell.imageView.image = [UIImage imageNamed:#"checkmark.png"];
//Add Gesture Recognizer
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
tapped.numberOfTapsRequired = 1;
[cell.imageView addGestureRecognizer:tapped];
cell.imageView.userInteractionEnabled = YES;
//[cell addSubview:cell.imageView];
return cell;
}
//Method controlling what happens when cell's UIImage is tapped
-(void)imageTapped:(UIGestureRecognizer*)gesture
{
UIImageView *selectedImageView=(UIImageView*)[gesture view];
UIImage *imageDefault = [UIImage imageNamed:#"checkmark.png"];
UIImage *imageRed = [UIImage imageNamed:#"checkmark(red).png"];
UIImage *imageGreen = [UIImage imageNamed:#"checkmark(green).png"];
if (selectedImageView.image == imageDefault) {
selectedImageView.image = imageGreen;
//cell.selected = true;
NSLog(#"Selected");
} else {
selectedImageView.image = imageRed;
//cell.selected = false;
NSLog(#"Deselected");
}
}

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

Image over UITableVIew Cell upon selection

I want to display an image over a cell upon selection. I've tried endless ways to do so without succes.
I've tried adding this to didSelectRowAtIndexPath:
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.imageView.image = [UIImage imageNamed:#"deselected_image.png"];
cell.imageView.highlightedImage = [UIImage imageNamed:#"selected_image.png"];
But the image won't change upon push.
Thanks
The code you have is creating a new table view cell - you need to retrieve the current cell that was selected. Try the following instead:
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:#"deselected_image.png"];
cell.imageView.highlightedImage = [UIImage imageNamed:#"selected_image.png"];
}
I have applied the UIColor to each cell on request.You can replace the UIColor code & apply the image.
Here is my code
- (void)tableView: (UITableView*)tableView willDisplayCell: (UITableViewCell*)cell forRowAtIndexPath: (NSIndexPath*)indexPath
{
if(tableView==tblLanguage)
{
cell.backgroundView = [[UIView alloc] init];
((UIView *)cell.backgroundView).backgroundColor = !(indexPath.row % 2)?[UIColor colorWithRed:(float)231/255 green:(float)231/255 blue:(float)231/255 alpha:1.0]:[UIColor colorWithRed:(float)218/255 green:(float)218/255 blue:(float)218/255 alpha:1.0];
((UIView *)cell.backgroundView).alpha=1.0;
//ilangSelectedIndex is set in the didselect row of the tableview delegate function
if(ilangSelectedIndex==[indexPath row])
{
((UIView *)cell.backgroundView).backgroundColor=[UIColor colorWithRed:(float)171/255 green:(float)177/255 blue:(float)213/255 alpha:1.0];
}
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
}
}
In the Did select function of the table you set the ilangSelectedIndex as Indexpath.row of your table & reload the table to see the effect.
Try this code in didSelectRowAtIndexPath:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.selectedBackgroundView=[[UIImageView alloc]init];
cell.backgroundView =[[UIImageView alloc] init];
((UIImageView *)cell.selectedBackgroundView).image=[UIImage imageNamed:#"selected.png"];

iOS - when to remove subviews from UITableViewCell

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

Resources