How do I draw to the contentView of a UITableViewCell? - iphone

I'm trying to draw a line on a custom UITableViewCell. When I draw inside the overridden drawRect and use the current graphics context I don't see anything.
I know I can't see anything because the draw rect is drawing to the UIView and not the contentView. So my question is, how do I draw to the content view using CG? Say, grabbing the graphics context to the contentView?

I have played with this in the past and what i have ended up doing is creating my own subclass of UIView and adding iot to a custom subclass of UITableViewCell and drawing to it that way. I like the control that this gives me.

For some reason (I don't know why, but I did notice this) if you "custom-draw" (ie, redefine drawRect) on what's in the 'contentView' of your cell, it does not show up on the table cell. However, the view that you assign to cell.backgroundView and cell.selectedBackgroundView show up perfectly well...
You could put your custom view in cell.backgroundView, put nothing in cell.contentView and it will show up fine (except for when you select the row, because then your view disappears and is replaced by cell.selectedBackgroundView).
So, what you can do is:
Use cell.contentView to show a custom view without any background
Use cell.backgroundView and cell.selectedBackgroundView to show a fancy background for your cell...
Or:
Make your custom view flexible enough so that it can show both the selected and non-selected state, and use 2 instances of your custom view: one in cell.backgroundView and one in cell.selectedBackgroundView

I found my answer here. Basically the super class ABTabeViewCell sets up the context so you can easily draw in the drawContentView function.

Related

Subclassing UITableViewCell for grouped table

I'm trying to create a a custom subclass of a UITableViewCell to be used in a grouped table.
I'm laying out the subclass with a nib. (I've just tried doing it without the nib but getting the same problem).
Whenever I've done this before I've wanted to create a whole new cell style with a different background etc... so in awakeFromNib I do this...
- (void)awakeFromNib
{
// N.B. I am not doing this, this is how I normally get rid of border
// in this case I want the border so I am not running this code.
self.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
self.backgroundColor = [UIColor clearColor];
}
but in this case I want to keep the background with the rounded corners and I'm just adding different UI elements to it.
The cell gets created and all the elements are laid out correctly but I'm getting an annoying "second border" in the top left corner of every section.
I have a screen shot showing this.
At first I thought this was a hair on my screen or something but it isn't.
I've tried setting the backgroundView and backgroundColor but that removes the normal cell background and I want to keep them.
Any ideas how I can get rid of this?
EDIT
Just to clarify what I said above about setting backgroundColor and backgroundView.
Here is what happens when I change them...
In this I set the backgroundView to nil. The border remains but so does the little bit I'm trying to get rid of.
If I set the backgroundView to a new UIView then this happens...
I want the same background with the white background and the rounded corners border. Just not with that annoying little bit.

iPhone: Drawing on a UIButton

This tutorial shows how to draw on a graphics context for the view using Quartz 2D:
http://www.techotopia.com/index.php/An_iPhone_Graphics_Drawing_Tutorial_using_Quartz_2D
But I want to draw on a UIButton, not on a view. How can I do that?
Thanks
Draw on UIView . Add the view as subview to your UIButton.
A button IS a UIView. It inherits from UIControl, which inherits from UIView.
So buttons have a drawRect method.
So you can do everything described in the article you linked on a button.
However, buttons are set up to do a lot of things for you, and overriding the drawRect method could make those things not work correctly.
Buttons normally draw a title and a rounded rectangle frame. You can turn that off by setting the button's type to custom.
Custom buttons will draw an image if you install one.
Buttons normally also either draw a highlight over their image, or have a second image to use for the highlighted state. If you want to implement drawRect, you'll need to handle drawing the highlighted state yourself.
In general, you want to avoid using drawRect and use some other technique to get the content you want into your views.
What, exactly, are you trying to do?

Background image that's scrollable and in synch with the UITable scrolling

I've added an image to a UITableview using the following code. What I can't figure out is how to have that background image scroll with the table (not an image in each cell, but a large image behind the UITable). I need the background image to be scrollable and in synch with the UITable scrolling.
I've searched and all the examples I've seen simply add a static image.
here's the code:
[self.view setBackgroundColor:
[UIColor colorWithPatternImage:
[UIImage imageWithContentsOfFile:
[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:
#"background.png"]]]];
thanks for any help.
I had this same problem and didn't find any solutions. I rolled my own which I explain in more detail than can be easily given here. It's explained at UITableView that scrolls the background with dynamic content
The basic idea is the following:
create a second table view behind your table view that has dynamic cell heights (or heights that aren't a multiple of a background pattern image's height)
turn its .userInteractionEnabled property to NO (so it doesn't receive touch events)
have it listen to the 'front' table view's contentOffset property via KVO so that the background table view sets its contentOffset to the same value.
have this background table view be its own delegate (you have to make a subclass so to implement KVO listening handlers anyway), and it populates empty cells' contentView.backgroundColor = [UIColor colorWithPatternImage: ...]
make sure your 'front' table view cells have transparent backgrounds. i.e. backgroundView set to an imageView without an image and opaque set to NO, and backgroundColor set to clearColor.
worked for me quite fine, and didn't seem in any way slower. The tables were still quite snappy on an iPod 4G
Because few days ago I saw that -
http://www.appuicomponents.com/component/sbtv
I am guessing the answer is not trivial. But maybe you would like to try that ?
Any way, if you are using a pattern image, isn't that possible to add the pattern to the cells them selfs ?

Rotating UIView and still seeing the old position of the view

I am rotating a view using src.transform = CGAffineTransformMakeRotation(M_PI/2);, it is working well but I am still seeing the old position of the view (like that: http://cl.ly/233y403c2C1C451r1f28).
How can I refresh the view ? The view is in a UITableViewCell, I tried to refresh the row of the cell or even the tableview but it doesn't work.
Here is my drawRect method :
if (nil != _image) {
[_image drawInRect:rect contentMode:self.contentMode];
} else {
[_defaultImage drawInRect:rect contentMode:self.contentMode];
}
The view that I am rotating is embedded in a UIImageView which is part of the cell.
Thanks,
Martin
It sounds like you might have two copies of your image view there. When you rotate the top one, the bottom one is exposed.
What is the background colour of the UIView you're overriding drawRect in? Before drawRect is called, the background colour is applied to the context. Ensure you have a background colour set (i.e. not set to [UIColor clearColor], for example).
Also, what's the opaque property of your UIView set to?
If figured it out. I was trying to rotate the image while I was downloading it. When I rotate it from a delegate callback that is call when the image is loaded everything works fine.
All of that makes perfect sense !

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.