How to change cell background image on click on cell in iphone????I have tried following but it is not working....
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0,0,320,50) reuseIdentifier:myid] autorelease];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"myImage.png"]];
}
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"myImage.png"]];
In the above piece of code you forget to autorelease; also have you set the delegate of the tableview? Set a breakpoint on the tableview:didselectRowAtIndexpath method to check if it is called or not.
BTW your code seems right.
You can change the selectionType of UITableView
using
cell.setSelectionStyle = [UITableViewSelectionStyleGray/Blue/None] (only 3 types)
some syntex error in above code try with proper syntex
by default it will be blue...
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"mImage.png"]]
Related
i want to animate tableviewcell background image with two blinking images on didSelectRowAtIndexPath. We can use following code, but it will show one image in background. However I want gif like image which blinks.
cell.backgroundView = [ [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"correctAnswer.png"]]autorelease];
UIImageView has the ability to do animation with the animationImages property.
UIImageView* imgView = [[[UIImageView alloc] init] autorelease];
imgView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"correctAnswer1.png"],
[UIImage imageNamed:#"correctAnswer2.png"], nil];
imgView.animationDuration = 0.5;
cell.backgroundView = imgView;
[imgView startAnimating];
You need to have each frame of the animation included in your project, correctAnswer1.png, correctAnswer2.png, etc...
Also, wouldn't it be better to use UITableViewCell's imageView property? That way you would get your animation in the left of the cell and it wouldn't interfere with the text or any other parts of the cell...
I have the following code which draws a separator line and text for a UITableViewCell. It looks fine but when I scroll off screen then back, the separator line is gone but the text is still fine. Any ideas?
static NSString *aProgressIdentifier = #"CustomerCell";
UITableViewCell *aCustomerCell = [iTableView dequeueReusableCellWithIdentifier:aProgressIdentifier];
if (!aCustomerCell) {
aCustomerCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:aProgressIdentifier] autorelease];
aCustomerCell.contentView.backgroundColor = [UIColor whiteColor];
UIImageView *aLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 72, 800, 1)];
aLine.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
[aCustomerCell addSubview:aLine];
[aLine release];
}
CMACustomer *aCustomerObject = aCellObject;
aCustomerCell.textLabel.text = aCustomerObject.customerFullName;
aCustomerCell.detailTextLabel.text = nil;
aCell = aCustomerCell;
Try to add the "aLine" image view as subview of the contentView and not the whole table itself. Probably when the cell is reused and then layoutSubviews is called again, the contentView overlaps (white background) your aLine. Infact consider that iOS default cells have their subviews dynamically redrawn and resized each time they are displayed on screen.
So I would try this:
[aCustomerCell.contentView addSubview:aLine];
If this doesn't work, what can you do is to remove the contentView completely and add your own custom subviews (do this inside the if(!aCustomerCell) and not outside unless you will not get the benefits of the cell re-use):
if (!aCustomerCell) {
aCustomerCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:aProgressIdentifier] autorelease];
[cell.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
UIImageView *aLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 72, 800, 1)];
aLine.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
[aCustomerCell.contentView addSubview:aLine];
[aLine release];
}
Finally another check is verify that the cell height is > 72 (it seems a trivial check but its often source of headaches!).
The table view is using a pool of cells, so you can't be sure which one you're getting for any given index path. You can use the cell or the content view, but be sure to add only one of your custom lines per cell.
UIImageView *aLine = (UIImageView *)[cell viewWithTag:64];
if (!aLine) {
// etc.
UIImageView *aLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 72, 800, 1)];
aLine.tag = 64;
[cell addSubview:aLine];
//
}
// other formatting logic here, you can also hide/show aLine based on biz logic
try adding it to the contentView
[aCustomerCell.contentView addSubview:aLine]
I'm working with a customized TableView and I have a problem when I click on a cell and it gets highlighted. After clicking on the cell, it gets highlighted and takes me to another view. The problem is that, when I return to the TableView screen, the cell is still highlighted.
Here is my code:
In cellForRowAtIndexPath
cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage
imageNamed:#"CustomCell.png"]] autorelease];
cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"CellHighlighted.jpg"]] autorelease];
In didSelectRowAtIndexPath
Characteres *vCharacters = [[Characters alloc] initWithNibName:#"Characters" bundle:[NSBundle mainBundle]];
//vCharacters.selectedCountry = selectedCountry;
[self presentModalViewController:vCharactersr animated:YES];
[vCharacters release];
vCharacters = nil;*
You need to add this to didSelectRowAtIndexPath:
[tableView deselectRowAtIndexPath:indexPath animated:NO];
This will deselect the row.
trying to figure out how to set the selected image for for a tableViewCell.
The old way of writing this was cell.selectedImage but that has been deprecated since 3.0.
I've tried numerous things, but can't get it too work.
Thanks!
Josh
According to the Deprecated UITableViewCell Methods doc, you should use the imageView's highlightedImage property as a replacement for selectedImage. For example:
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.imageView.image = [UIImage imageNamed:#"deselected_image.png"];
cell.imageView.highlightedImage = [UIImage imageNamed:#"selected_image.png"];
You can set selected backgroundView like below....
UIImageView *selBGView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"urimage.png"]];
cell.selectedBackgroundView = selBGView;
That didn't worked because you might set the normal state image like this
cell.imageView.image = ///
Try it - It worked for me perfectly!!
UIImageView *selBGView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"urimage_selected.png"]];
cell.selectedBackgroundView = selBGView;
[selBGView release];
UIImageView *BGView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"urimage.png"]];
cell.backgroundView = BGView;
[BGView release];
Try this ...
UIImage *bgImage = [UIImage imageNamed:#"icon_call.png"];
UIImageView *bgImageView = [[UIImageView alloc] initWithImage:bgImage];
[bgImageView setFrame:CGRectMake(280, 2, 30, 38)];
//Finally give this imageView to the cell
[cell.contentView addSubview:bgImageView];
Hope it will solve your problem !
Use this one!:
selectionBackground = [UIImage imageNamed:#"background.png"];
cell.selectedBackgroundView =[[UIImageView alloc] init];
((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;
i can display BG image in table view cell but when i click any cell there is a blue color filling all my cell and my images just goes behind now i want to show my image in front of that blue color how to do that??
cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"SuperCell.png"]] autorelease];
Maybe you can use
cell.selectionStyle = UITableViewCellSelectionStyleNone;
and
cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"SuperCell.png"]] autorelease];