How to change `UIImageView` in custom cell in `UITableView` - ios

I have UIImageView in custom cell in UITableView and in want to change that image in didSelectRowAtIndexPath
It's working with UIImageView outside the UITableView, but not working inside it.
I am using this code..
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *string = [[self.itemsInTable objectAtIndex:indexPath.row] objectForKey:#"Name"];
ExpandCell *cell = [self.menuTableView dequeueReusableCellWithIdentifier:#"Cell"];
UIImage *image;
if ([string isEqualToString:#"News"]) {
if ([[self.viewImage image] isEqual:[UIImage imageNamed:#"down-arrow.png"]]) {
NSLog(#"down");
image = [UIImage imageNamed:#"up-arrow.png"];
} else {
NSLog(#"up");
image = [UIImage imageNamed:#"down-arrow.png"];
}
cell.imgExpand.image = image;
self.viewImage.image = image;
}
}
How can I fix that?

First assign a tag number to your UIImageView (say 10).
Then write this code in your didSelectRowAtIndexPath method
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIImageView *yourImageView = (UIImageView*)[cell viewWithTag:10];//Replace yourImageView with the name of your UIImageView on custom cell
//--------- Your code ------
NSString *str = [[self.itemsInTable objectAtIndex:indexPath.row] objectForKey:#"Name"];
UIImage *image;
if ([str isEqualToString:#"News"])
{
if ([[self.viewImage image] isEqual:[UIImage imageNamed:#"down-arrow.png"]])
{
NSLog(#"down");
image = [UIImage imageNamed:#"up-arrow.png"];
}
else {
NSLog(#"up");
image = [UIImage imageNamed:#"down-arrow.png"];
}
[yourImageView setImage:image];
[self.viewImage setImage:image];
}
I think this will resolve your problem.

The following line creates a new cell:
ExpandCell *cell = [self.menuTableView dequeueReusableCellWithIdentifier:#"Cell"];
Change it to:
ExpandCell *cell = [tableView cellForRowAtIndexPath:indexPath];
This will give you the currently visible cell you just selected.

Add this command [tableView deselectRowAtIndexPath:indexPath animated:YES]; at the end of didSelectRowAtIndexPath

After you set a new image,you can use function:
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
reload the cell you changed.

Related

Change custom cell image color when selected in uitableview?

I have a custom cell with imageview and label.When user selected a particular cell in the tableview I want change the image color or tint.
I managed to change the color of the label , but dont know how to do with the image. Any ideas?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"MenuCell";
MenuTableViewCell *cell = (MenuTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[MenuTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
// cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.lblTitle.text = [[_items objectAtIndex:[indexPath row]] valueForKey:#"title"];
cell.imgIcon.image = [UIImage imageNamed:[[_items objectAtIndex:[indexPath row]] valueForKey:#"image"]];
cell.lblTitle.highlightedTextColor = [UIColor colorWithRed:0.839 green:0.682 blue:0.047 alpha:1];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
return cell;
}
Thanks
You should be changing the cell data inside your MenuTableViewCell custom class. There will be a methods in there that control the selected, highlighted state. The method will look something like this example,
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
if (selected) {
//Change the text colour & image
} else {
//Change it back to whatever is was
}
}
If you want to change cell image when you selected a cell. You can draw current image with a tintColor. See below code.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
MenuTableViewCell *cell = (MenuTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
// Change cell image color
UIImage * image = [cell.imgIcon.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
cell.imgIcon.image = image;
cell.imgIcon.tintColor = [UIColor redColor]; // Your tint color
[cell.imgIcon tintColorDidChange];
}
Hope that helps!
In the method tableviewDidSelectRowAtIndexPath write this code
//Edited to the correct syntax
{
MenuTableViewCell *cell = (MenuTableViewCell *)[tableview cellForRowAtIndexPath:indexPath];
cell.image = [UIImage imageNamed:#"your image name when you want to change to selected"];
}
The correct syntax is the following:
In the method tableviewDidSelectRowAtIndexPath
MenuTableViewCell *cell = (MenuTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.imgIcon.image = [UIImage imageNamed:#"your image name when you want to change to selected"];
Saheb Roy's answer put me on the right track but cellAtIndexPath has to be replaced by cellForRowAtIndexPath.
EDITED: in the above code what was being done was to have two different images and change them depending on whether the cell was selected or not.
Combining the answers given by Saheb Roy, longpham and Devster101 finally i added the following code in custom cell class MenuTableViewCell.m:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
if (selected) {
UIImage * image = [_imgIcon.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
_imgIcon.image = image;
_imgIcon.tintColor = [UIColor colorWithRed:0.839 green:0.682 blue:0.047 alpha:1];
[_imgIcon tintColorDidChange];
} else {
UIImage * image = [_imgIcon.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
_imgIcon.image = image;
_imgIcon.tintColor = [UIColor blackColor];
[_imgIcon tintColorDidChange];
}
}

SDWebImage UIImageView+WebCache in custom UIImageView

I'm having a problem with loading images from my webserver into my UIImageView in a tableview
This code works perfect, but the images are placed weird and ugly in my tableview:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Get the cell. Note that this name is the same as in the storyboard
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//Set the correct name in the cell.
//Do so by looking up the row in indexpath and choosing the same element in the array
NSInteger currentRow = indexPath.row;
Image* currentImage = [self.images objectAtIndex:currentRow];
// Here we use the new provided setImageWithURL: method to load the web image
[cell.imageView setImageWithURL:[NSURL URLWithString:currentImage.src] placeholderImage:[UIImage imageNamed:#"testimage.jpg"]];
return cell;
}
So I created an UIImageView on my storyboard, and changed the code to this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Get the cell. Note that this name is the same as in the storyboard
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//Set the correct name in the cell.
//Do so by looking up the row in indexpath and choosing the same element in the array
NSInteger currentRow = indexPath.row;
Image* currentImage = [self.images objectAtIndex:currentRow];
UIImageView *imgView = (UIImageView *)[cell viewWithTag:100];
// Here we use the new provided setImageWithURL: method to load the web image
[imgView setImageWithURL:[NSURL URLWithString:currentImage.src] placeholderImage:[UIImage imageNamed:#"testimage.jpg"]];
return cell;
}
But if I run this code on my device, I'm getting a signal SIGABRT error.
How can I get this working on my custom Imageview?
In the first code snippet, add these lines:
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.imageView.clipsToBounds = YES;
You are getting SIGABRT because your imagevIview is NOT added to cell, but cell's contentView..! So the right way to get the imageView should be,
UIImageView *imgView = (UIImageView *)[cell.contentView viewWithTag:100];

Update UITableView Cell imageView by Tag

Is it possible to update the cell imageView in an iOS UITableView by tag? I appreciate other methods are available but I would like to use tags if possible.
I set the image by:
cell.imageView.image = [UIImage imageNamed:#"dinner.png"];
And in another method I can get the cell by (Tags are always unique):
NSInteger the_tag = ((UIView*)sender).tag;
UITableViewCell *updateCell = (UITableViewCell*)[tableView viewWithTag:the_tag];
Is it possible to update the cell with this method? This does not work:
updateCell.imageView.image = [UIImage imageNamed:#"lunch.png"];
EDIT:
On reflection, the tags are bad method to use. You can use (Which will give you the row and the section if you have a sectioned table):
- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath {
NSLog(#"Tapped Row %#", indexPath);
UITableViewCell *cell = [self->tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:#"lunch.png"];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = (CustomCell*)[tblView1 cellForRowAtIndexPath:indexPath.row];
UIImageView *imageView = cell.imageView;//CustomCell you have imageView as #propertyVariable..
imageView.image = [UIImage imageNamed:#"image.png"];
}
I hope it will be helpful for you...
try calling [updateCell setNeedsLayout] or [updateCell.imageView setNeedsLayout]
Thank you for all the answers. On reflection, I feel tags are a bad method to use. I have updated my original post with what I feel is a better solution.
- (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];
cell.tag = indexPath.row;
cell.imageView.image = [UIImage imageNamed:#"icon.png"];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int tag = 2;//this is same at table cell row number.
UITableViewCell *cell = (UITableViewCell*)[tableView viewWithTag:tag];
cell.imageView.image = [UIImage imageNamed:#"image.png"];
}

I would like to scroll my CollectionViews both horizontal and vertical. Can't figure out how to do it

I have a Gallery Page where I want to have 12 unique CollectionViews that scroll independently. This I can do, but I also want them to scroll vertically as a group. It seems like they need to be in a tableCell. But I can't seem to get this to work. I'm using Story Boards. So the question is how do you put the output of a CollectionView into a tableCell, with each tableRow holding a unique CollectionView.
This the trick to having collectionView swipe horizontal and independent. Each one is "tagged". Now I just need to go vertical too.
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath: (NSIndexPath *)indexPath {
NSLog(#">>> Entering %s <<<", __PRETTY_FUNCTION__);
if (cv.tag == 0) {
MyCell *cell = [cv dequeueReusableCellWithReuseIdentifier:#"CELL" forIndexPath:indexPath];
UIImage *theImage = [UIImage imageNamed:[flowerImages objectAtIndex:indexPath.item]];
cell.imageView.image = theImage;
return cell;
} else if (cv.tag == 1) {
MyCell *cell = [cv dequeueReusableCellWithReuseIdentifier:#"CELL2" forIndexPath:indexPath];
UIImage *theImage = [UIImage imageNamed:[carImages objectAtIndex:indexPath.item]];
cell.imageView.image = theImage;
return cell;
} else {
MyCell *cell = [cv dequeueReusableCellWithReuseIdentifier:#"CELL3" forIndexPath:indexPath];
UIImage *theImage = [UIImage imageNamed:[birdImages objectAtIndex:indexPath.item]];
cell.imageView.image = theImage;
return cell;
}
}
Ok, I ended up using Static Table Cells. Worked!

Changing a UIImage in custom tableviewcell

I have a tableview that is made up of custom tableview cells which contain an imageview. In cellForRowAtIndexPath I set the image of each cell. I want the image to change when the cell is selected, so in didSelectRowAtIndexPath I get the cell and change the image, no problems there. However, when I scroll the table (read: table reloads the cells) the new image is no longer present. Also when the cell is no longer selected I want the image to switch back to the original.
I have tried doing the following in cellForRowAtIndexPath:
if(cell.isSelected){
cell.imageview.image = [UIImage ImageNamed: #"selected.png"];
}
else
cell.imageview.image = [UIImage ImageNamed: #"not selected.png"];
I have also tried using the BOOL values cell.highlighted or cell.selected to no avail.
Any thoughts are appreciated.
Try using a class variable selectedCellIndexPath of type NSIndexPath. In didSelectRow... you set it's value, and in the cellForRow... you write:
if([selectedCellIndexPath isEqual:indexPath]){
cell.imageview.image = [UIImage ImageNamed: #"selected.png"];
} else {
cell.imageview.image = [UIImage ImageNamed: #"not selected.png"];
}
Edit:
Or you could simply write:
if([indexPath isEqual:[tableView indexPathForSelectedCell]]){
cell.imageview.image = [UIImage ImageNamed: #"selected.png"];
} else {
cell.imageview.image = [UIImage ImageNamed: #"not selected.png"];
}
but the first solution is a little more efficient.
If you have an array with the Name of the Images it would be the easiest to change the name at the selected index in the array and reload the table... Like This:
myImageArray = [[NSMutableArray alloc] init];
[myImageArray addObject:#"name1.jpg"];
[myImageArray addObject:#"name2.jpg"];
// etc..
And the in the cellForRowAtIndexPath Method:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
[cell.imageView setImage:[UIImage imageNamed:[myImageArray objecAtIndex:indexPath.row]]];
}
In the didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[myImageArray setObject:#"newName.jpg" atIndexedSubscript:indexPath.row];
[tableView reloadData];
}

Resources