Reputation: 22415
I am creating a UITableView which has multiple types of cells.So I creating multiple cell using xib and for each cell I have one .h file one .m file and one .xib
file.All these cells has some common things like cell background color,property(UILabel,UIView) and actions(UIButton click).
So I am doing a set of common things again and again.So how can I create a super class of these cell so that I can come out of the problem.I should be linked the custom xib cell to my super class.
Suppose I have created a Super Cell
subclass of UITableViewCell having the all the common properties of those cells in its header file (SuperCell.h) and also implemented all the common actions in its implementation file (SuperCell.m).Then I Made all those .xib cell header file as a subclass of my SuperCell. Now how can linke these .xib files header property to the SuperCell header property which is same.
Thanks @Fogmeister for pointing me out that it will be a big hierarchies and difficult to maintain.And If I want to add some new label in child cell then I am also not clear where should I add and how to linked with the super cell.
Let me clear my question explaining my project a little bit.
I am creating an social app like facebook which has Text post,single image post,double images post,multiple images post, poll,event,etc
.
So for my social app landing page I have a UITableview controller and all these type of post is linked to one one cells.All these cell has some common things like Post ownername(UILabel),@handler (UILabel),profile pic ( ImageView) ,Post time (UILabel),like button (UIButton),comment button (UIbutton) etc.
I have done everything and it is working fine
.I have written a lot of common code for setting up all these cell as there is not SuperCell of these cells.So I am trying to figure out a solution to make it little bit easier.
Thanks
Upvotes: 0
Views: 364
Reputation: 77631
Ah, I see your problem now. As a very first starting point I can think of two possible (maybe three) ways of approaching your issue.
(N.B. everything here is just me using the Facebook app as an example, your actual app may differ).
At the moment you have different cells StatusCell
, PhotoCell
, VideoCell
, ShareCell
, etc...
Each of these have various different elements... userNameLabel
, userAvatarImage
, timeLabel
, likeButton
, commentButton
.
Then each has a "contentArea" that contains the status, photo, video, url, etc...
The first approach I was thinking is to keep your different cell types but then to create UIView
subclasses to easily populate the areas. So instead of the cells having the different user labels and images etc... create a view called UserDetailsView
.
This UserDetailsView
will take a single property of a User
object. It then uses this object to populate the different labels it contains such as userNameLabel
, userAvatar
etc...
Now you can just add this view to each different cell type.
You can also create components for the ShareView
which might include likes, comments, etc...
In addition to creating these different components for each different type of Cell you could actually use a single type of cell. (This would only work if the content is in roughly the same place for each).
So the additional part to create now are the different content views. This might be a StatusView
, PhotoView
, etc...
Now you can use one generic cell type that has a space for a content view. (Maybe placed inside a container view for positioning and constraints).
What Facebook does for their timeline is to use the React Native framework that they have created for immutable view hierarchies. This is a more complex method as it requires reworking the way you build stuff but definitely one to keep in mind for the future.
https://facebook.github.io/react-native/
Upvotes: 1
Reputation: 14030
create your "parent cell":
@interface SuperCell : UITableViewCell
@end
@implementation SuperCell
// background logic and all the stuff that is equal for your child cells
@end
with it's .h and .m files.
then create your "child cells" (with their .h and .m files) and make them inherit from your "parent cell":
@interface SomeCustomCell : SuperCell
@end
@interface AnotherCustomCell : SuperCell
@end
and so on...
Upvotes: 0