Uitableviewcell Imageview Changing on Select

UItableViewCell imageview changing on select

I've face exactly the same issue as you,and I fix

the issue is cause by you named your imageView "imageView"

i don't know why

if you change your imageView name like "m_imageView"

the issue fixed

It's all about naming!

Change image on selection in UITableViewCell(swift 3 xcode)

One of the solution would be that you need to maintain a seperate list of rows you selected, compare them in cellForRowAt method.

Code would look something like this.

var selectedArray : [IndexPath] = [IndexPath]()

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let myCell = tableView.cellForRow(at: indexPath) as! TableCell
myCell.resourceIcons.image = UIImage(named: "RubiusResources2")
tableView.deselectRow(at: indexPath, animated: true)

if(!selectedArray.contains(indexPath))
{
selectedArray.append(indexPath)
}
else
{
// remove from array here if required
}
}

and then in cellForRowAt, write this code to set proper images

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
.
.
.

if(selectedArray.contains(indexPath))
{
// use selected image
}
else
{
// use normal image
}

.
.
.
}

UITableView change image on cell select and reset others

You have issues on a number of fronts.

First, the information which determines what image should be displayed should be part of your model. So, when a row is selected, set a flag in your model to say that this item is tagged. At the same time, find the other items that are tagged and untag them. Now, reload the rows that have changed. In cellForRowAtIndexPath, update the requested cells to set the standard or the alternative image based on the tag info from the model.

Second, for your use of tag - you are getting mixed up between imgCell_Marker and imageView. When you do:

ALog(@"%i", [[[tableView cellForRowAtIndexPath:indexPath] imageView] tag]);

you are querying the default imageView, so the tag will always be zero. You should be using viewWithTag:TAG_IMAGEMARKERCELL to find the actual image view you want.

Change image on select within tableview cell - Swift

The issue is probably that you are calling dequeueReusableCellWithIdentifier. Try using cellForRowAtIndexPath instead (documentation about it here). This will give you a reference to the current cell, rather than a reference to a new reusable cell.

So, according to your code above:

let cell = self.tableView.cellForRowAtIndexPath(indexPath) as! CustomTableViewCell

This typecast is critical because cellForRowAtIndexPath returns UITableViewCell, which will not have the bgImage member.

Also, make sure that you have a member called bgImage that is a UIImageView in your CustomTableViewCell class by adding @IBOutlet strong var bgImage: UIImageView!, and that said IBOutlet is connected in your storyboard/xib.

UITableView cell Imageview changing its size on click?

If you reload the tableview when it finish loading, the image get the size that it was suppose to have

[self.tableview reloadData];

Change Imageview image in tableview cell at selected index

If you change your cell imageView in didSelectRowAtIndexPath so when you scroll your tableView it mismatched.

so my suggestion is that add selected indexPath in NSMutableArray and in cellForRowAtIndexPath check that array contains selected indexPath if it contains than set radio_check image otherwise set radio_uncheck image like below. Define NSMutableArray globally.

NSMutableArray *arrSelectedImages = [[NSMutableArray alloc] init];

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
VoteDeatailTableViewCell *cell = [_voteTable dequeueReusableCellWithIdentifier:@"VoteDeatailTableViewCell"];
cell.contentView.backgroundColor = [UIColor clearColor];

if ([arrSelectedImages containsObject:indexPath]) {
cell.imgRadio.image = [UIImage imageNamed:@"radio_check"];
}
else {
cell.imgRadio.image = [UIImage imageNamed:@"radio_uncheck"];
}

return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[arrSelectedImages removeAllObjects];
[arrSelectedImages addObject:indexPath];
[self.tblVW reloadData];
}

How to change a UITableViewCell image when the cell is selected?

UITableViewDelegate has 3 methods to handle cell highlighting:

- tableView:shouldHighlightRowAtIndexPath:
- tableView:didHighlightRowAtIndexPath:
- tableView:didUnhighlightRowAtIndexPath:

Try like this :

- (BOOL)tableView:(UITableView*)tableView shouldHighlightRowAtIndexPath:(NSIndexPath*)indexPath
{
return YES;
}

- (void)tableView:(UITableView*)tableView didHighlightRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell* cell = (UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:@"Highlightimage"];
}

- (void)tableView:(UITableView*)tableView didUnhighlightRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell* cell = (UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:@"image"];
}


Related Topics



Leave a reply



Submit