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;
Related
I want to add an UIActivityIndicator to an UITableViewCell. How can I do?
try like this
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"NoReuse"] autorelease];
cell.textLabel.text = #"Loading...";
UIActivityIndicatorView *spinner = [[[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] autorelease];
// Spacer is a 1x1 transparent png
UIImage *spacer = [UIImage imageNamed:#"spacer"];
UIGraphicsBeginImageContext(spinner.frame.size);
[spacer drawInRect:CGRectMake(0,0,spinner.frame.size.width,spinner.frame.size.height)];
UIImage* resizedSpacer = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
cell.imageView.image = resizedSpacer;
[cell.imageView addSubview:spinner];
[spinner startAnimating];
return cell;
UIActivityIndicatorView *spinner = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
spinner.frame = CGRectMake(205.0f, 0.0f, 44.0f, 44.0f);
spinner.tag = 3;
[cell.contentView addSubview:spinner];
I'm wondering how to set an imageview to the center of a tableview cell dynamically.
Here is my current code.
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100,100, 20, 20)];
imgView.image = [UIImage imageNamed:#"friendsCelluserpic.png"];
cell.imageView.image = imgView.image;
Try this code :
This will align your image horizontally centre in TableViewCell
float imgwidth=40;
float imgheight=40;
imgView = [[[UIImageView alloc] initWithFrame:CGRectMake((cell.contentView.bounds.size.width/2)-(imgwidth/2),15, imgwidth, imgheight)] autorelease];
[cell.contentView addSubview:imgView];
try this, :)
UIImageView * imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"friendsCelluserpic.png"]];
imgView.frame=CGRectMake(100, 100, 20, 20);
[cell.contentView addSubview:imgView];
return cell;
you can code like this [cell.contentView addSubview:imgView];
I have Created custom cell.And i have table view with Grouped style.Now i want to put Backgroung image to each cell of different section.How to set the background image of custom cell.
Here is the code for creating the custom cell.
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 305, 90)];
// view.backgroundColor=[UIColor darkGrayColor];
UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(11, 9, 61, 55)];
// NSLog(#"imageArray%#",[play.imageArray count]);
NSString *img=[play.imageArray objectAtIndex:indexPath.row];
NSLog(#"img=%#",img);
imageV.image = [UIImage imageNamed:img];
[view addSubview:imageV];
[imageV release];
UILabel *lblAchievementName = [[UILabel alloc] initWithFrame:CGRectMake(90, 10, 168, 21)];
lblAchievementName.text = [play.listData objectAtIndex:indexPath.row];
lblAchievementName.backgroundColor=[UIColor clearColor];
lblAchievementName.textColor=[UIColor orangeColor];
[lblAchievementName setFont:[UIFont fontWithName:#"Arial Black" size:16.5]];
[view addSubview:lblAchievementName];
[lblAchievementName release]
[cell.contentView addSubview:view];
return cell;
Now how to set the backgroung image for this custom view?
You can try this code that will help you.
Put this code in cellForRowAtIndexPath method
UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 277, 58)];
av.backgroundColor = [UIColor clearColor];
av.opaque = NO;
av.image = [UIImage imageNamed:#"categorytab1.png"];
cell.backgroundView = av;
set thru setBackgroundView method.Form the ImageView with ur need Image aand set that as,
[cell setBackgroundView:urBgImgView];
You can use following code for different image in different section.
UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(11, 9, 61, 55)];
// NSLog(#"imageArray%#",[play.imageArray count]);
NSString *img=[play.imageArray objectAtIndex:indexPath.row];
NSLog(#"img=%#",img);
if (indexPath.section == 0) {
imageV.image = [UIImage imageNamed:img0];
}
else if (indexPath.section == 1) {
imageV.image = [UIImage imageNamed:img1];
}
else if (indexPath.section == 2) {
imageV.image = [UIImage imageNamed:img2];
}
[view addSubview:imageV];
[imageV release];
i'd rather to add a CALayer or UIImageView to be the backgroud view. Because i used to set the BackgroundColor, but it never workd for me. so i changed my mind by this way.
I want to display background image on tableview cell... on that i need to display my data.
How to do that?
#thanks in advance... Any proficient help me out.
Do u want set background image for your table or for individual cell in UITableView
If it is for setting the background image then do the following:
UIImage *bgImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"background.png" ofType:nil]];
UIColor *bgColor = [[UIColor alloc] initWithPatternImage:bgImage]; self.view.backgroundColor = bgColor;
[tableName setBackgroundColor:[UIColor clearColor]];
where tableName is the name of your UItableView
If you want to set background image for UITableView cell then do the following:
cell.backgroundColor = [UIColor clearColor];
UIImageView *cellBG_tap = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"image.png" ofType:nil]]];
cell.backgroundView = cellBG_tap;
[cellBG_tap release];
set a class on the cell, then use CSS to put the background image in, like below:
<table class="myTable">
<tr>
<td class="myCell"></td>
</tr>
</table>
<style>
.myCell {
background: url(my_image.gif)
}
</style>
One way is to modify the UITableViewCell's contentView. For example:
//Add custom objects to the cell in here!
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image.jpg"]];
imageView.center = CGPointMake(310, 48);
[cell.contentView addSubview:imageView];
cell.textLabel.text=#"test";
set background image as
UIImageView* img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"yourImage.png"]];
[cell setBackgroundView:img];
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
cell.textLabel.font =[UIFont systemFontOfSize:20.0];
cell.detailTextLabel.textColor=[UIColor whiteColor];
cell.detailTextLabel.font = [UIFont systemFontOfSize:15.0];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.imageView.image = [UIImage imageNamed:#"Square.gif"];
}
in table view data source
cellForRowAtIndexPath
if (indexPath.row%2==0) {
UIImageView *img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"image..."]];
cell.backgroundView=img;
[img release];
I used this its working .... #all Thanks people for contributing your knowledge
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"]]