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
答案 0 :(得分:1)
(N.B。这里的一切只是我用Facebook应用程序作为例子,你的实际应用可能会有所不同)。
目前您有不同的单元格StatusCell
,PhotoCell
,VideoCell
,ShareCell
等...
每个元素都有各种不同的元素...... userNameLabel
,userAvatarImage
,timeLabel
,likeButton
,commentButton
。
然后每个人都有一个“contentArea”,其中包含状态,照片,视频,网址等......
我想到的第一种方法是保留不同的细胞类型,然后创建UIView
子类以轻松填充区域。因此,不是具有不同用户标签和图像等的单元格,而是创建一个名为UserDetailsView
的视图。
此UserDetailsView
将采用User
对象的单个属性。然后,它使用此对象填充其包含的不同标签,例如userNameLabel
,userAvatar
等...
现在您可以将此视图添加到每个不同的单元格类型。
您还可以为ShareView
创建可能包含赞,评论等的组件......
除了为每种不同类型的Cell创建这些不同的组件之外,您实际上可以使用单一类型的单元格。 (这只有在内容大致相同的情况下才有效。)
因此,现在创建的附加部分是不同的内容视图。这可能是StatusView
,PhotoView
等等......
现在,您可以使用一个具有内容视图空间的通用单元格类型。 (可能放在容器视图中进行定位和约束)。
Facebook为其时间表所做的是使用他们为不可变视图层次结构创建的React Native框架。这是一个更复杂的方法,因为它需要重新设计构建内容的方式,但绝对要记住未来。
答案 1 :(得分:0)
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...