how to display image to background to tableview Cell - iphone

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

Related

UITableViewCell background image not displaying

I am trying to add a background image to UITableViewCell using the following code:
UIImage *bgImage=[UIImage imageNamed:#"green-cell-row.png"];
UIImageView *bgForFirst=[[UIImageView alloc] initWithImage:bgImage];
cell.backgroundView=bgForFirst;
[cell.contentView addSubview:venueDisplayImage];
[cell.contentView addSubview:venueDisplayName1];
[cell.contentView addSubview:venueDisplayName2];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
However, I get venueDisplayName on white space around it. What would be the best way to fix this and correctly add my background image?
Try this way:
1) TabelView set the background like:
tblView.backgroundView.alpha = 0.0;
tblView.backgroundColor=[UIColor clearColor];
2) Add the image view in cellForRowAtIndexPath og delegate method of tableview:
UIImageView *bckView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0, 300,80)];
cell.backgroundView.alpha = 0.0;
cell.backgroundColor=[UIColor clearColor];
bckView.image = [UIImage imageNamed:#"cell.png"];
bckView.contentMode = UIViewContentModeScaleToFill;
[cell.contentView addSubview:bckView];
Did you try to change backgroundColor or venueDisplayName to [UIColor clearColor] or to [UIColor greenColor]?
For performance reasons try to avoid using non-opaque views in UITableViewCell.
You have to use the property of cell
there are 2 property you can use
1) cell.backgroundColor
2) cell.backgroundView
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageName:#""]];
In second case you have to set any view as a background view

Setting colors of UITableViewCell and UITableView background

I was trying to use a background image for my TableView so I don't get the leftover tableview cells when there isn't enough data to populate the whole table on screen, so I tried to follow this URL, but much simpler: http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html
I do this in the viewcontroller that has an outlet to my UITableView:
UIImage *backgroundTableImage = [UIImage imageNamed:#"form_background.png"]; // basically a gray->white gradient
UIImageView *tableBackgroundImageView = [[UIImageView alloc] initWithImage:backgroundTableImage];
tableBackgroundImageView.frame = self.TableView.bounds;
self.TableView.backgroundView = tableBackgroundImageView;
[tableBackgroundImageView release];
self.backgroundColor = [UIColor clearColor];
Then I have a custom subclass of UITableViewCell that I create in IB. In IB, it's background is set to White-default. My cells are all transparent, and I only see my background. How do I get my cells to stay white.
At first take a view in your .h file UIView *newView; declare its property and synhesize it at your .m file
Now in cellForRowAtIndexPath
if(cell==nil){
tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"imagename.png"]];
................
.........
newView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
[cell.contentView addSubview:newView];
................
...............
}
if(indexPath.row == 0){
newView.backgroundColor = [UIColor blueColor];
}
if(indexPath.row == 1){
newView.backgroundColor = [UIColor redColor];
}
if(indexPath.row == 2){
newView.backgroundColor = [UIColor greenColor];
}
If any further question please knock me.

How to set Background image in UITableView cell?

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.

UITableViewCell Set Selected Image

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;

How to customize tableView Section View - iPhone

I know how to customize the tableViewCell.
I have seen many application customizing the tableView Cell.
Similarly, I want to customize TableView Section Header
"Suppose - A section name should be in different font, It has different background images etc."
Is it possible How?
In which method Should I implement the code?
Instead of using the normal method
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
You want to implement this one:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
As you can see, the second one returns a UIView instead of just string for text. Therefore you can customise your own view (with labels etc) and return it.
Here is an example of how you might do that (to be implemented in the above method):
// create the parent view that will hold header Label
UIView* customView = [[[UIView alloc] initWithFrame:CGRectMake(10,0,300,60)] autorelease];
// create image object
UIImage *myImage = [UIImage imageNamed:#"someimage.png"];;
// create the label objects
UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.font = [UIFont boldSystemFontOfSize:18];
headerLabel.frame = CGRectMake(70,18,200,20);
headerLabel.text = #"Some Text";
headerLabel.textColor = [UIColor redColor];
UILabel *detailLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
detailLabel.backgroundColor = [UIColor clearColor];
detailLabel.textColor = [UIColor darkGrayColor];
detailLabel.text = #"Some detail text";
detailLabel.font = [UIFont systemFontOfSize:12];
detailLabel.frame = CGRectMake(70,33,230,25);
// create the imageView with the image in it
UIImageView *imageView = [[[UIImageView alloc] initWithImage:myImage] autorelease];
imageView.frame = CGRectMake(10,10,50,50);
[customView addSubview:imageView];
[customView addSubview:headerLabel];
[customView addSubview:detailLabel];
return customView;