Universal cell for UITableView and UICollectionView - ios

I was wondering if there are some best practices for implementing universal cell which can be used with both of UITableView and UICollectionView...
I tried to put UITableViewCell into CollectionView but Xcode complained about this. I have just one variant left: make UIView which will implement cell's view and then implement UITableViewCell and UICollectionViewCell subclasses which will wrap this custom UIView so it could be placed into Table/Colleciton view. Is that the right way to achieve my goal?
Any thoughts on this?

Your idea can be implemented if the UIView's subviews are similar.You can make a template of UIView,create multiple widgets inside it,and create instance in UITableViewCell and UICollectionViewCell,as UITableViewCell and UICollectionViewCell are usually reused, you need to separate the data model and views,so realize your idea.

Related

How to make a reusable view cells for UITableViewCell and UICollectionViewCell ? I've tried to sublassing UIView for the cell but it's not working

I want to reuse my UITableViewCell to my UICollectionViewCell.
Until now, I always use 2 xibs, 1 for UITableViewCell and 1 for UICollectionViewCell and I think it's kinda waste.
I've tried to subclassing UIView for the cell but it's not working.
A way to do this is by using reusable views. Here is a Github project that can help: NibDesignable
You can also do this without using the project I mentioned, but it does make it easier. You then create a UIView with a View nib and put all your UI into this as well as any code for setting the UI components. Then you create your UITableViewCell and UICollectionViewCell and then just use the view that you created. You can also use prototype cells in a Storyboard for CollectionView or TableView if you prefer.

Is possible to share a xib between a UITableViewCell and a UICollectionViewCell?

I am looking to factorize some code and xib for a view design shared between a UITableViewCell and UICollectionViewCell. Is it possible share the same xib between a UITableViewCell and UICollectionViewCell? or Is it possible to share the same xib for a subview?.
you could do something like this by just using a UIView super view. Then each of those cells would just need to load the shared view into the cells. I think you may lose the ability for auto layout to work with UIAutomaticDimension for autosizing cells.

iOS alternative for Androids <include> tag?

How am I supposed to reuse views in iOS?
For example, I have a view that returns as a CollectionViewCell, inside of a UITableViewCell and on a specific spot in a UIViewController.
What is the recommended way to deal with this?
Do I make a xib and add a holder view everywhere I need it?
But, then to react to size changes, I need to add constraints in code, which seems to be too much hassle just for a simple inclusion of a xib.
Is there a feature in Xcode I can use?
Create an UICollectionViewCell in a XIB. Then this cell is placed inside an UICollectionView. You can place this UICollectionView basically everywhere, also inside an UITableViewCell. As the UICollectionView can be auto-layout constraint in the Storyboard the size changes automatically. No need for programmatic constraints. When you want to reuse the UICollectionViewCell just put it inside another UICollectionView in another UIViewController or UITableViewCell.
Btw: to use this just as "reusing"-feature you can disable the scrolling in the UICollectionView.

UITableViewCell customization, where to attach subviews?

when subclassing UITableViewCell to define a complete custom cell what style to set? UITableViewCellStyleDefault? Actually I don't need neither the label or the imageView, would they be allocated the same?
So when attaching my custom views to the UITableViewCell subclass, is it better to add to self, or to self.contentView?
And is it ok to do some quartz drawing in drawRect in the UITableViewCell subclass (Like background) ?
Thanks
when subclassing UITableViewCell to define a complete custom cell what style to set? UITableViewCellStyleDefault? Actually I don't need neither the label or the imageView, would they be allocated the same?
Since you are not using any of the pre-defined UILabel or UIImageView subviews of UITableViewCell, it doesn't really matter. I just use UITableViewCellStyleDefault. I do not know for sure if the UILabels or the UIImageView are allocated or not. However, since there is no way to explicitly tell the UITableViewCell not to create those subviews, I wouldn't worry about it. Even if they are created, you aren't using them so drawing the cell will not be any slower, and since cells are reused as you scroll through the table, allocating just a few extra objects once is not going to affect your overall table view performance.
So when attaching my custom views to the UITableViewCell subclass, is it better to add to self, or to self.contentView?
All of the content should be put in the UITableViewCell contentView. This will ensure that the contents behave properly when the UITableView has an index on the right or if the UITableView (or UITableViewCell) go into editing mode.
And is it ok to do some quartz drawing in drawRect in the UITableViewCell subclass (Like background) ?
The recommended approach is for the background to be placed in the UITableViewCell backgroundView property. So I would not recommend doing any drawing in the UITableViewCell subclass itself. Just use the contentView, backgroundView, and selectedBackgroundView properties. You can create a simple UIView subclass and just set that as the backgroundView of the UITableViewCell, if you like.
If you want to create a custom UITableViewCell, my experience is that it's much easier to use IB instead of code--even if you prefer using code normally. I would definitely recommend taking a look at these two links:
Creating a custom UITableViewCell in iOS 4
Custom UITableViewCell
This sounds like exactly what you're looking for -- you don't have to use the label or the image in this case, and you can make outlets to any custom UIViews that you need.
what style to set? UITableViewCellStyleDefault?
it depends on your aims. The most used style is UITableViewCellStyleDefault.
See Using Cell Objects in Predefined Styles
is it better to add to self, or to self.contentView?
developer.apple.com recommends to add subview to self.contentView (they specially call it as contentView).
is it ok to do some quartz drawing
It also depends on your aims. If you use only images or colors for background, then customize your cell view in IB, I don't see the reasons to use qurtz framework.

iOS custom and scrollable UITableView header and UITableViewCell

I want to know how to make a horizontal scrollable UITableView header and UITableViewCell like the app "JP Morgan Fund Watch."
The right part of the UITableView header and UITableViewCell can be scrolled. Any idea how to make it?
You can achieve that effect with using UIScrollView as tableViewHeader and content area of custom UITableViewCell subclass but thinking it is trivial to implement would be naive.
Check the following link for details.
http://theexciter.com/articles/touches-and-uiscrollview-inside-a-uitableview.html

Resources