How to Change an UIimageview into a Custom Cell

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

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.

iOS swift UIImageView change image in tableView cell

Try code its working 100%

  var selectindex : Int?
var selectedindex : NSMutableArray = NSMutableArray()
@IBOutlet var tableview: UITableView!

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("LikeCell", forIndexPath: indexPath)

let like: UIButton = (cell.viewWithTag(2) as! UIButton)
let comment: UIButton = (cell.viewWithTag(3) as! UIButton)
if selectedindex.containsObject(indexPath.row) {
like.setBackgroundImage(UIImage.init(named: "like.png"), forState: .Normal)
}else{
like.setBackgroundImage(UIImage.init(named: "like (1).png"), forState: .Normal)
}
comment.setBackgroundImage(UIImage(named: "chat.png"), forState: UIControlState.Normal)
like.addTarget(self, action: #selector(self.CloseMethod(_:event:)), forControlEvents: .TouchDown)
comment.addTarget(self, action: #selector(self.CloseMethod1(_:event:)), forControlEvents: .TouchDown)

return cell

}

@IBAction func CloseMethod(sender: UIButton, event: AnyObject) {

let touches = event.allTouches()!
let touch = touches.first!
let currentTouchPosition = touch.locationInView(self.tableview)
let indexPath = self.tableview.indexPathForRowAtPoint(currentTouchPosition)!
selectindex = indexPath.row
if selectedindex.containsObject(selectindex!) {
selectedindex.removeObject(selectindex!)
}else{
selectedindex.addObject(selectindex!)
}
self.tableview.reloadData()
}

Change image of custom cell with UIImagePickerController

You're creating a new cell here:

let myCell = ImageOfPlaceViewCell()

Instead of that you need to change image of an existing cell. You need to get an existing cell object from the table. To do this you can use tableView.cellForRow, and pass there index path of your row.

I'm not sure about your table structure, but you also need to make sure that when you reload your table it won't disappear, so you can store picked image somewhere else to use next time in cellForRowAt.

var pickedImage: UIImage?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: ImageOfPlaceViewCell.identifierOfImageOfPlaceCell) as! ImageOfPlaceViewCell
cell.imageOfPlace.image = pickedImage
}
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
let image = info[.editedImage] as! UIImage
pickedImage = image

let myCell = tableView.cellForRow(at: IndexPath(row: 0, section: 0))

myCell?.imageOfPlace.image = info[.editedImage] as! UIImage

picker.dismiss(animated: true)
}

p.s. Also I'm not sure what's this let cell = tableView.dequeueReusableCell(withIdentifier: "cell")! before your if, it's probably redundant(at least in case when you return cell from that if)

UIImageView in custom cell not working as it should

It turns out the UITableViewCell already has an ImageView in it named imageView and my custom image view had the same name. When I was trying to set the image in it the default imageView of the cell got the image instead of my own UIImageView.
I changed the name of my imageView to ImgThumb and put the image in it and worked perfectly.

Changing a UIImage in custom tableviewcell

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.

How to Change an UIImageView Image in a Table cell, on a button click from the cell?

Keep this method in your arsenal...

- (NSIndexPath *)indexPathWithSubview:(UIView *)subview {

while (![subview isKindOfClass:[UITableViewCell self]] && subview) {
subview = subview.superview;
}
return [self.tableView indexPathForCell:(UITableViewCell *)subview];
}

Then,

- (IBAction)pressed:(id)sender {

NSIndexPath *path = [self indexPathWithSubview:(UIButton *)sender];
LocationCardCell* cell = (LocationCardCell *)[self.tableView cellForRowAtIndexPath:path];

// carry on happily

But not too happily. You question doesn't state what you plan to do with that cell once you have it. The generally right approach to modifying tableview cells is to modify your model, reload the cell, and then account for that change in the datasource (tableView:cellForRowAtIndexPath:).

A more conventional pattern would be to use the indexPath we just found to dereference your model, modify it, then reload.

Changing custom UIImage in didSelectRowAtIndexPath

Create cell from index path

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

var cell = tableView.cellForRowAtIndexPath(indexPath) as! CustomCell

cell.indicator.image = UIImage(named: "newImage")

}


Related Topics



Leave a reply



Submit