Drawing on a UITableViewCell backgroundView using CoreGraphics - iphone

I have a subclass of UITableViewCell where I need to draw a custom backgroundView for the cell. Implementing drawRect:, I can draw the view fine, but it is behind the tableView and tableViewCells.
In order to use CG to draw my custom backgroundView, do I need to make a separate UIView subclass that I do the drawing in and set that as the cell's backgroundView?
Thanks!
EDIT: Screenshot
I want the blue to be drawn INSTEAD of the white. In place of the white. Or at least on top of it.
I am drawing a shadow behind the cell using CA, and I would like to have this shadow behind my view I am drawing.

You need to set the background of the contentView.
myCell.contentView.backgroundColor = [ UIColor greenColor ]; //

try this,
cell.contentView.backgroundColor = [UIColor clearColor];

Related

Different colors for grouped tableView and tableViewCell backgrounds

How can I set different colors for GROUPED UITableView inside background vs. background, which is outside my table? That is the part which is visible when tableView is smaller than the screen or scrolled beyond limits of table.
UPDATE: that sample image is generated by my current code! I DO NOT NEED TIPS HOW TO MAKE IT !!! Please read the question before you (try to) answer. Would really appreciate this, but thanx anyway.
File AboutViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.backgroundView = [[CustomView alloc]
initWithFrame:self.tableView.bounds]; // VISIBLE
self.tableView.backgroundColor = [UIColor redColor]; // NOT VISIBLE
}
In the sample image (generated by my current code) the background view contains sample gradient from yellow to green, each cell "bubble" background is texture, each label has own background - all this just to demonstrate what I could define. As you can see, background gradient is partially visible through each cell and remains static when you scroll the table.
What I want to do: I want DIFFERENT tableView background for GROUPED UITableView.
For plain UITableView style I have defined a custom background for each cell, but it doesn't seem to work with GROUPED tableView. This only sets the cell background inside the cell "bubble". How to define the cell area outside that cell "bubble"?
For this you can use the image as backgroundimage.
But I think you are using the grouped table,so
you need to go with the tableview background color
and set that to image color.
Like
winTableView.backgroundColor=[UIColor colorWithPatternImage:#"bg.png"];
Or you can even go with the Gradient layer,inserting that in you view.
Hope this will help you.
Get the image of different color as you want to show the background of your table view.
For this u set cell.backgroundColor =[UIColor redColor]; its work
and for background u tale image or set color
For cell color or view you can also use these.
Try to have different color for different cells by your logic.
cell.backgroundColor=[UIColor clearColor];
//cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.accessoryType=UITableViewCellAccessoryNone;
UIView *backroundSelecView=[[[UIView alloc]init]autorelease];
backroundSelecView.backgroundColor=[UIColor colorWithRed:203.0/255.0f green:218.0/255.0f blue:140.0/255.0f alpha:.80];
cell.selectedBackgroundView=backroundSelecView;
cell.backgroundView=[[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"table-grid#2x.png"]]autorelease];

UITableViewCell - How to set selection clipping?

In my app, I have a UITableView and each UITableViewCell utilizes a custom background and style with the following code in the "WillDisplayCell" method:
UIImage *cellBackgroundImage = [UIImage imageNamed:#"TableView_Cell_Background_iPhone"];
UIImageView *backgroundImage = [[UIImageView alloc] initWithImage:cellBackgroundImage highlightedImage:cellBackgroundImage];
[cell setBackgroundView:backgroundImage];
[backgroundImage release];
The problem is that the PNG I'm using as the background is a rectangle, but the UITableViewCell is a rounded rectangle with a specific layer radius property. When I select a cell, the background overrides the rounded nature of the cell and I get a jarring blue highlighted sharp rectangle. Is there a way to set the selected state corner radius or something along those lines? My only other option if not would be to create a PNG background that fits the rounded rectangle perfectly.
Thanks.
I would set the selectionStyle of the cell to UITableViewCellSelectionStyleNone, this stops the blue highlight. Then start to do a custom selection job.
You will have to make a custom subclass of UITalbeViewCell. Override setSelected or if that doesn't work user a gesture recognizer or override touchesBegan etc. I have done it once, before, I forget exactly how, if you have trouble let me know and ill look it up.
When you detect that the cell is selected, perhaps create a translucent overlay as a PNG and make it appear when the cell is selected. Alternatively lower the transparency of the background image or add a color mask.

How to put gradient style table cell in a table?

Hey, I am making a custom table cell right now but my custom cell is just white cell with some buttons and labels in background color which looks not that great.
Is there a way to render the background color so that the cell has some vertical gradient effect?
For example, on top of the cell it looks white and as it gets closer to center of the cell, it gets darker.
But as it gets closer to the bottom of the cell, it gets whiter again.
A great example would be this free app called "friendsaroundme".
Another thing is that I don't want to use custom image to do it (i.e make the cell.backgroundcolor = [uicolor colorwithpatternimage:somethingsomething.png..... ) because it's not that flexible.
So is there anyway to render the gradient style programatically?
You can use a CAGradientLayer as the background layer. Remember a table view cell is just a UIView. You can add a new layer to the layer tree with:
[[[self view] layer] addSublayer:gradientLayer];
I wrote a blog post on how to do this with UIButtons. The same technique applies.

Is there a way to fix the background image of a UITableView

I have set the background of my UITable with a custom image.
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]];
The problem I'm having is the background image is scrolling and doesn't look good at the top and bottom of the screen where you see the image being joined.
Is there a way to fix the position of the background image and just have the table scroll over the top of it?
Insert a UIImageView behind the UITableView. Set the UIImageView's image to background.png. Use [UIColor clearColor] as the table's background.
You need to put a UIImageView beneath your table view, set the table view's background color to clear (or in IB, set the opacity property of its background at 0). Then construct your CELL views to have clear areas that your background image can be seen through. You can do that programmatically or in IB (it's easier to visualize in IB, but the code to hook it all together is a bit tricky).

Black corners on UITableView Group Style

I have a problem where my UITableView (group style) has a black "tip" above it's rounded corner.
I'm setting up the background of the tableview like so:
[meetingTableView setBackgroundColor:[[UIColor alloc] initWithPatternImage:[[UIImage alloc] initWithContentsOfFile:#"background.png"]]];
And my table view ends up looking like this:
black pointy edge on rounded corner http://papernapkin.org/pastebin/resource/images/imageEntryId/6487
Any ideas how I can get rid of those black points?
I have got the same problem.
When I set clear color by xib, I have the back corner
The solution is to set it by code !
(The same problem with interface builder exist for webviews)
Try this in your controller's viewDidLoad method:
meetingTableView.backgroundColor = [UIColor clearColor];
You'll get Black corners on UITableView Group Style if you set background color to clear color in XIB.
Instead try this code for removing Black corners on UITableView Group Style
tableViewObject.backgroundColor=[UIColor clearColor];
Just in case you weren't already aware, there's another neat technique you can use to make customized backgrounds for UITableViews:
Not quite as simple as setting the background as you're doing, but it gives you a lot more flexibility and can scale to any table size.
Maybe if you put yourTableViewOutlet.backgroundView=nil;
To avoid the black corners you have to make sure that the UITableViewCells are not opaque. It looks like you're using custom styles table cells and the default value for opaque is YES. Either go to Interface Builder and uncheck the opaque checkbox if the table cell was set up in a XIB file. Or use the setOpaque:NO setter to change value.
Because the table cell view still has a rectangular frame even with the rounded corners the cell view and not the actual table view is causing those black corners.
My guess is that it's related to something that you're doing in your custom table view cells. You might want to experiment with setting the cell background color to [UIColor clearColor].
I think you should set the background color of your table as clearColor and initialsie your view with the background image.
Then it will definitely not show the black corners. But also don't forget to set the background color of your cell as white color
The up-voted answer above (set the tableView's background to [UIColor clearColor]) may not work for you if you are like me and never use the UITableViewController, instead putting a UITableView inside a UIViewController.
In this case it's not the tableView that needs to have a clear background, but the view that holds the tableview.
This is not intuitive, but it works for me. In interface builder you can just set the parent view's background color to clear color, or you could do the same in code in viewDidLoad with:
self.view.backgroundColor = [UIColor clearColor];
I'm guessing the reason for the black corners is something about the internal graphics optimization, and setting the background clear fixes it.