Drawing a custom UITableViewCell - uitableview

I have to draw two horizontal lines in a cell, one in the vicinity of the edge at the top and the other near the button one. These two lines should be very pale yellow, that is the same color as the background view.
I must to create a subclass of UIView or a subclass of UITableViewCell?

You can subclass a UIView, create custom DrawRect code inside it and load it as the cells background view. Here is an excellent tutorial on how to do this.

Related

Custom-drawn UIView as UITableViewCell Image

We have a simple UITableView where we're using the default style since we just need a label and an image. However, we need our image to be custom-drawn.
We of course can't insert a UIView with a custom drawRect implementation as UITableViewCell expects a UIImageView, not a UIView.
We also tried subclassing UIImageView and overriding its drawRect but UIImageView doesn't call drawRect as its expecting its image data to come from an actual image, not a drawing routine.
Is there perhaps a way I can create a custom-drawn image and use that as the source for a UIImageView, which I can then simply pass to the UITableViewCell?
In lieu of that, the two (nasty) workarounds I've come up with are either
Perform custom drawing on the background view in UITableViewCell, or
Add our custom-drawn UIView to the view hierarchy of UITableViewCell's ContentView, bypassing using the built-in image capabilities.
Am I missing an easier way to achieve this?

ios tableView clearColor seperator

I have set my tableView's background color, and I want there is space(10px) around each cell show that color.
e.g.(ignore the red line)
What I am doing now is make the cell embed a smaller view, and set the cell's background to be clear. But it is annoying to create such embed view again and again.
So, I am seeking for a better solution, I believe it would be good if i can increase separator's height. But the old answers did not solve my problem.
(How to increase the UITableView separator height?)
EDIT:
However, if I add my own separator in cell. I may not able to set the cell having round corner
Set the tableView's separatorStyle to .None.
Create a custom UITableViewCell subclass.
In the storyboard (or xib if you created one for the custom cell), add a subview that's inset the amount that you want padded on all four sides.
If you want rounded views, do that on the custom cell's subview (e.g. subview.layer.cornerRadius = 10.0, subview.layer.masksToBounds = true).
Set the custom cell's ContentView to have a clear background.
Set the table view's background to that lovely shade of blue.
Set the cell subview's background color to white.
You don't have to use the cell repeatedly in InterfaceBuilder. Just give the cell an ID, and in your implementation of the table view's data source, dequeue a cell instance using that ID and set the values of the labels inside it.
Have you thought of just making the cell height taller and putting your own separator on the bottom of your custom cells? Nice table layout by the way.

UITableViewCell overlap shadow on top of cell directly below

I'm trying to create a simple effect where a cell casts a shadow on top of the cell directly below (in a vertical UITableView). Much like a tiled roof where the top tile is layered above the tile below and casts a shadow on the cell below.
I've tried overriding layoutSubviews in UITableView and played with bringSubviewToFront etc but that did not work. The shadow works somewhat but disappears as you tap on a cell (somehow the selected view overlaps it).
Any way to do this easily?
It seems the only reliable way is to draw the 'shadow' on the cell below the top one and then keep track of global selection states. A second option is to use UICollectionView instead but that's an overkill.
For cell dropping shadow on the one below:
Add shadow to the layer of a view in your cell.
For all cells dropping shadows only on the background view
Add shadow to the layer of the tableview itself.

iOS: UIView backgroundColor in table view

So this problem is pretty weird and a little difficult to describe. Essentially I have a UITableView where each cell has an image, and a UIView covering part of the image. The UIView has it's background colour set to about 30% transparent white, giving it the appearance of washing out the image beneath. There's also some labels and such on it. All these views are added to a single parent container view, which is finally added to the cell's contentView. This all works fine - until I try to select the cell. For some reason, whenever the cell is highlighted, the backgroundColor property on each and every one of those views is set to clear, until the cell is unhighlighted (at which point the old colour returns).
Can anyone explain why this happens, and more important if there's a way to fix it? At most I could create a single pixel image of white, turn the UIView into a UIImageView, and set it's image to that pixel - but that's inelegant at best. Any suggestions?
When you select the cell, the implementation of setSelected:animated: is called and set the background color of all the views included in this cell to transparent.
Simply, you can just disable the selection of the cell by setting its selectionStyle to None:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
Or, if you want to use the result of selection, you can override the setSelected:animated: method and reset the background color of your UIView

Drawing in screen with finger in UITableView

I am following this excellent small tutorial about drawing on screen in the layer of a UIView (subclass).
http://spritebandits.wordpress.com/
It just works.
There is just one thing. I placed this view as subview of a UITableViewCell which, naturally, is displayed as part of a UITableView.
I guess I would have the same issue when I would place it within an UIScrollView. (UITableView inherits from UIScrollView anyway)
Touches are triggered by my painting view as long as their related movement is horizontal. As soon as I move the finger kinda vertical, even partly, then the UITableView takes over and scrolls the table.
Is there any proper way of stopping the Table to taike control of the touches while the touch is actually within my view?
If it is of importance: I am using storyboard. The cell is a prototype cell with its own subclass of UITableViewCell.
I've implemented this using the same class "Canvas" you're saying and also inside a UITableViewCell. But I didn't use the entire cell for drawing, only a UIView inside of the UITableView as a subview.
I reached the whole user experience by activating and deactivating the UITableView scrolling when that UIView (subview of the cell where I allow the drawing) fires touchesBegan or touchesEnded. So when they touch/move inside the UIView, they're drawing. When it's outside, they're scrolling. So they can't scroll on the UIView because they will be drawing.
The problem in your case is that since the whole cell is the view for drawing, the user cannot scroll in this concrete cell because it's the drawing one.
Hope this will help.
I dont have an answer for that. But there is essentially a work around.
You could get the user in to a drawing mode, where the scrolling for a UItableviewcell is disabled and then
once the user is done with drawing, you could enable scrolling again.
Inclusion of a button anywhere on the screen would help you switch modes.

Resources