New UITableViewCell not calling subview drawRect() - ios

I am having this problem for the past two days and I'm trying to get my head around it with no good result.
I have a UITableView with custom UITableViewCell. Inside the UITableViewCell I have a custom UIView that I subclassed and drawing it by calling its own drawRect.
The drawing for reused cells has no issues and the cell is calling its own drawRect and the custom subview is calling its own drawRect as well.
The problem arises when a new cell is dequeued, the custom subview drawRect is not called at all leaving that view empty (non visible)
What I did:
calling subview.setNeedsDisplay has no effect, it's redrawing the reused cells' custom subview but it has no affect on drawing the newer cells' custom subviews.
That's the only thing I thought of and was obvious to try.
Is there a way to force drawing for the subviews for new UItableViewCell?

Have you tried calling [cell setNeedsDisplay] but on the main thread?

I managed to solve the issue and force the redrawing of the subview of a custom cell by implementing heightForRowAtIndexPath regardless of the iOS version (>=7)
Apparently iOS redraw the cell after the height is being calculated.

Related

How to preLoad UICollectionVewCell?

I have all the UICollectionView dataSource, but the subViews in cell is not all the same.So need remove the old subViews and add the subViews when the cell showing again. Want to know any way to preload UICollectionViewCell or Make it more smoothly.
Instead of "preloading" the cells, I would suggest subclassing UITableViewCell for each of your different types of cells, and adding the subviews in their awakeFromNib method. I would recommend against adding subviews in your tableview:cellForRowAtIndexPath: method, you should just be updating the content (e.g. UILabel values).
If you add subviews inside tableView:cellForRowAtIndexPath" method, you will need to remove all of the subviews first. Otherwise if they are "reused" the subviews will be added on top of the existing ones.

Change frame of UIView in UICollectionViewCell in cellForItemAtIndexPath

My problem is very similar to this one, though the selected correct answer (which is just to use tags) didn't work for me (nor did any other answer): Changing label position in UICollectionViewCell.
I have a UICollectionView with a custom UICollectionViewCell. The cell has a UIView in it, which is referenced via IBOutlet property. I'm simply trying to change the frame within the cellForItemAtIndexPath method, and while the frame of the UIView is in fact changed, it is subsequently ignored, or not honored.
However, once the UICollectionView is scrolled a bit and it begins reusing cells, the reused cells do honor the previous change to the UIView's frame, and the UIView looks just like it should. So for some reason the first time cellForItemAtIndexPath is called for a particular cell, the frame change is ignored, but is subsequently honored once the cell is reused.
Actually, something more curious which I just noticed is that a reused cell has the size of the frame that had been previously changed, but keeps the original origin, which happens to be (0, 0), though I'm trying to update it to something like (0, 50).
Changing other properties of the UIView works fine, such as changing the background color. What might be happening to prevent the UIView's frame from initially changing?
UICollectionViewCell does not update frames for views that are defined in storyboard or xib files.
You have to create the view programmatically in your custom cells initWithFrame and/or awakeFromXib files.
Cheers!
hmmmm
I may be mistaken. I know this is true for UITableViewCell
Cannot update UI object's frame in UITableViewCell

Semi-Selection of Custom UITableViewCell Corrupts UILabel Drawing

Tapping and then rolling your finger off of my UITableView's custom UITableViewCell corrupts the drawing of the contained UILabel. Neither setSelected: nor setHighlighted: are called. The UITableView uses dynamic prototypes. Tapping the cell results in normal selection and drawing. Here is a video showing what happens:
http://www.youtube.com/watch?v=vMHQc5tpcOY
I do custom drawing in a subclassed CALayer. Any guidance in resolving is appreciated.
I was able to resolve this by overriding setHighlighted as described in this answer.

Monotouch: Force UITableView to recalculate height of each cell without reloading data

I have a custom cell created using OwnerDrawnElement with autoresizeable UITextView in it.
When text changed there should be appropriate layout redraw and cell height recalculation.
The problem is how to preserve keyboard opened.
There is a method inside UITableView - ReloadRows which actually helped me in some fashion.
I can't call it for my cell because it is the first responder and can't I cant resign it.
But when I call it for another cell my cell is getting resized as I wanted but I have unnecessary another cell redraw.
So I wonder what method is called to relayout UITableView NOT reload data?!
The same method is probable called when you scroll up and down, cells become visible and height is recalculated.
I've tried standard SetNeedsDisplay(), SetNeedsLayout(), ReloadInputViews(), LayoutSubviews(), but it didn't do the same. Maybe I need use them somehow differently. I've tried to call them for cell and whole tableview objects.
I've looked into ReloadRows method and found out that it calls some API stuff:
Messaging.void_objc_msgSendSuper_IntPtr_int(base.SuperHandle, UITableView.selReloadRowsAtIndexPathsWithRowAnimation_, nSArray.Handle, (int)withRowAnimation);
So it doesn't what method forces tableview to recalculate height of each cell without reloading data either.
Can you help me with that?
Try to update cells frames, then call UITableView's empty update block:
tableView.BeginUpdates();
tableView.EndUpdates();

Subviews of UITableViewCell are not visible while reordering

When I reorder my UITableViewCells, the Subviews of the Cell are not visible while dragging...I get always the same result, whether I add the Subviews programmatically in a UITableViewCell Subclass or in Storyboard...
Is there a possibility to see the real UITableViewCell with Subviews while dragging?
Ok I found a solution by my self...
For some reason the mysterious reordering animation from Apple cant capture simple UIViews, but with UIImageViews, UIButtons and UILabels as Subviews of the custom TableViewCell, the animation works like expected...creepy!

Resources