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"];
}
Related
The question sums it up-- I'm trying to have a box get checked when someone clicks on the UITableViewCell, which involves changing the image in the cell. The table is set up well and works just fine-- it's displaying the information that I want it to, and the cells select like they should. However I can't get the image to change when the method didSelectRowAtIndexPath is called.
Here's what I have so far. I've removed extraneous code (from other tables, etc), but this should be everything that's relevant.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if (tableView == diabetesVisitTable) {
if (indexPath.section==0) {
cell.textLabel.text = [_microvascularValues objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:#"Family Med Unchecked Box.jpg"];
}
else if (indexPath.section==1) {
cell.textLabel.text = [_macrovascularValues objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:#"Family Med Unchecked Box.jpg"];
}
else if (indexPath.section==2) {
cell.textLabel.text = [_bloodSugarValues objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:#"Family Med Unchecked Box.jpg"];
}
cell.backgroundColor = [UIColor colorWithRed:0.75 green:0.52 blue:0.53 alpha:1.0];
self.diabetesVisitTable.backgroundColor = [UIColor colorWithRed:0.75 green:0.52 blue:0.53 alpha:1.0];
return cell;
}
else return 0;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
cell.imageView.image = [UIImage imageNamed:#"Family Med Checked Box.jpg"];
}
In your tableView:didSelectRowAtIndexPath: implementation, you're asking the table view for a new cell to configure with dequeueReusableCellWithIdentifier:. You want to fetch the actual cell used for that indexPath so you can update it directly:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:#"Family Med Checked Box.jpg"];
}
You'll also need to update the data source for the table view somehow to record the fact that one of the rows is selected, and update tableView:cellForRowAtIndexPath: to set the checked image if the data source indicates that the row is selected. If you don't do those steps, the cell will appear unselected when it refreshes.
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"];
}
You're trying to update the cell at the didSelectRowAtIndexPath function which returns void. So it will not update the tableview.
You should take the indexpath.row value from the diddSelectRowAtIndexPath and pass it to the cellForRowAtIndexPath. if they're matching, update the cell, it will work.
Also make sure you add a beginUpdate and endUpdate to reflect the table value reload
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];
I have a populated TableView where i would like to change the image. When I'm inside cellForRowAtIndexPath I can change it by using:
cell.imageView.image = [UIImage imageNamed:#"image.png"];
But I cannot figure out how to do the same thing inside the void, I have tried:
[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0]].imageView.image = [UIImage imageNamed:#"image.png"];
But it doesn't change the image at all.
Thanks,
/DSDeniso
EDIT:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
_row = indexPath.row;
[self changeImage];
}
- (void)changeImage{
UITableViewCell * cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_row inSection:0]];
if(cell)
{
cell.imageView.image = [UIImage imageNamed:#"image.png"];
} else {
NSLog( #"why is cell null? Did I set row properly?");
}
}
Just pass the cell to the method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
_row = indexPath.row;
[self changeImageForCell:cell];
}
- (void)changeImageForCell:(UITableViewCell*)cell{
cell.imageView.image = [UIImage imageNamed:#"image.png"];
}
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];
}
I would like to know if it is possible to assign some kind of icon/image to the navigation table cells on the left side of the UISplitViewController in an iPad App.
e.g. an image next to row 1...
http://www.cre8ive.kr/blog/wp-content/uploads/SplitViewTest_03.png
I am trying to approach this with editing the
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
method.
Here's what I got
// Customize the appearance of table view cells.
- (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] autorelease];
}
cell.textLabel.text = [NSString stringWithFormat:#"%i", indexPath.row +1];
//Let the magic happen here :D
UIImage *image = [UIImage imageNamed:#"deviantart_dark.png"];
//Nothing changes :(
[[cell imageView] setImage:image];
return cell;
}
Any ideas?
You could use the below code,
cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage:imageNamed:#"deviantart_dark.png"]];