在UITableViewCell和UICollectionViewCell之间共享代码 [英] Sharing code between UITableViewCell and UICollectionViewCell

查看:135
本文介绍了在UITableViewCell和UICollectionViewCell之间共享代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的UITableViewCell子类,它处理各种手势和stat行为。
我也在构建一个UICollectionView,我的UICollectionViewCell子类的行为非常接近我的UITableViewCell。我已经粘贴了很多代码。



我的问题是:是否有一个设计模式可以让我在这两个子类之间共享UI代码(手势和状态)?



我听说过组合模式,但是我很难适应这种情况。它是正确的使用模式吗?



注意:我必须保持UITableView和UICollectionView,所以删除UITableView不是一个解决方案。

解决方案

我想,你可以在他们共同的祖先UIView上使用类别。您只能分享常用的方法,而不是实例变量。



让我们来看看如何使用它。



您有自定义UITableViewCell

  @interface PersonTableCell:UITableViewCell 
@property(nonatomic,weak)IBOutlet UILabel * personNameLabel;
- (void)configureWithPersonName:(NSString *)personName;
@end

@implementation PersonTableCell
- (void)configureWithPersonName:(NSString *)personName {
self.personNameLabel.text = personName;
}
@end

和UICollectionViewCell

  @interface PersonCollectionCell:UICollectionViewCell 
@property(nonatomic,weak)IBOutlet UILabel * personNameLabel;
- (void)configureWithPersonName:(NSString *)personName;
@end

@implementation PersonCollectionCell
- (void)configureWithPersonName:(NSString *)personName {
self.personNameLabel.text = personName;
}
@end

两种共享方法 configureWithPersonName: / strong>到他们的祖先UIView允许创建类别。

  @interface UIView(PersonCellCommon)
@property(nonatomic,弱)IBOutlet UILabel * personNameLabel;
- (void)configureWithPersonName:(NSString *)personName;
@end

@implementation UIView(PersonCellCommon)
@dynamic personNameLabel; //告诉编译器信任我们有getter / setter某处
- (void)configureWithPersonName:(NSString *)personName {
self.personNameLabel.text = personName;
}
@end

现在在单元格实现文件中导入类别标题并删除方法实现。从那里可以使用类别中的常用方法。
您唯一需要复制的是属性声明。


I have a pretty big UITableViewCell subclass which handle various gestures and stat behavior. I'm also building a UICollectionView, my UICollectionViewCell subclass behavior is pretty close to my UITableViewCell. I've pasted a lot of code from it.

My questions is: Is there is a design pattern that would allow me to have the UI code (gesture and state) shared between those 2 subclasses ?

I've heard of the composition pattern, but I have hard time fitting it for this case. Is it the right pattern to use ?

Note: I MUST keep both UITableView and UICollectionView, so dropping the UITableView is not a solution.

解决方案

I think, you can use category on their common ancestor UIView. You can only share common methods, not instance variables.

Lets see how to use it.

For example you have custom UITableViewCell

@interface PersonTableCell: UITableViewCell
@property (nonatomic, weak) IBOutlet UILabel *personNameLabel;
- (void)configureWithPersonName:(NSString *)personName;
@end

@implementation PersonTableCell
- (void)configureWithPersonName:(NSString *)personName {
    self.personNameLabel.text = personName;
}
@end

And UICollectionViewCell

@interface PersonCollectionCell: UICollectionViewCell
@property (nonatomic, weak) IBOutlet UILabel *personNameLabel;
- (void)configureWithPersonName:(NSString *)personName;
@end

@implementation PersonCollectionCell
- (void)configureWithPersonName:(NSString *)personName {
    self.personNameLabel.text = personName;
}
@end

Two share common method configureWithPersonName: to their ancestor UIView lets create category.

@interface UIView (PersonCellCommon)
@property (nonatomic, weak) IBOutlet UILabel *personNameLabel;
- (void)configureWithPersonName:(NSString *)personName;
@end

@implementation UIView (PersonCellCommon)
@dynamic personNameLabel; // tell compiler to trust we have getter/setter somewhere
- (void)configureWithPersonName:(NSString *)personName {
    self.personNameLabel.text = personName;
}
@end

Now import category header in cell implementation files and remove method implementations. From there you can use common method from category. The only thing that you need to duplicate is property declarations.

这篇关于在UITableViewCell和UICollectionViewCell之间共享代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆