【问题标题】:How can I have two custom UICollectionViewCell classes share the same variable name如何让两个自定义 UICollectionViewCell 类共享相同的变量名
【发布时间】:2016-10-14 09:53:16
【问题描述】:

我有两个自定义 UICollectionViewCell 类,但是,我想在同一范围内创建一个具有相同名称的变量,因为 cellForItemAtIndexPath 方法中的所有属性和所有代码对于两个自定义单元格都是相同的。

我想做这样的事情: 例如:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{


CustomCell1/CustomCell2 *cell;

if (condition) {
    cell = (CustomCell1 *)[collectionView dequeueReusableCellWithReuseIdentifier:kCustomCell1Identifier forIndexPath:indexPath];
} else {
    cell = (CustomCell2 *)[collectionView dequeueReusableCellWithReuseIdentifier:kCustomCell2Identifier forIndexPath:indexPath];
}

    // Remaining code is same for both custom cells
    cell.property1 = value1;
    cell.property2 = value2;
    .
    .
    .
    .
    .
    //lot of code
    cell.property100 = value100;

return cell;
} 

我这样做的目的是不想重复所有剩余的代码。我知道创建通用自定义单元类并在该通用类中编写此条件可能是解决方案,但在该类初始化之前如何将条件变量传递给自定义类。因为我认为我们无法初始化(alloc init)UICollectionViewCell。

条件(三元)运算符可以做到这一点吗?

【问题讨论】:

  • 使用基类并在其中添加常用方法并从该类派生单元格。这将解决您的问题。
  • 子类或协议可以工作。当我看到cell.propertyN = valueN 其中 N 是从 0 到 100 时,我建议创建一个方法来处理单元格内的对象(NSArray ? NSDictionary)。
  • @Larme 我给出了 cell.propertyN = valueN 只是作为一个例子。实际上,有很多代码,例如分配图像,隐藏和显示不同的子视图,更改颜色等。
  • 我认为@ShaileshChandavar 的评论会对你有所帮助。
  • 为什么要创建具有相同属性的相同类?这两个类是否相同?如果不是,请创建一个具有通用 ivar 和方法的父类,然后创建继承父类的子类

标签: ios objective-c swift oop uicollectionview


【解决方案1】:

您可以创建一个基(父)collectionviewCell 类

@interface BaseCollectionViewCell : UICollectionViewCell 
//Add the common methods(can add common IBOulet and IBAction) and implement them.
@end

现在您可以创建从 BaseCollectionViewCell 派生的新集合视图单元格:

@interface ChildCell : BaseCollectionViewCell
//add methods which are specific to the ChildCell as the methods of    
//BaseCollectionViewCell is already available via inheritance.
@end

您可以通过以下方式创建另一个集合视图单元格:

@interface ChildCell2 : BaseCollectionViewCell
//add methods which are specific to the ChildCell2 as the methods of    
//BaseCollectionViewCell is already available via inheritance.
@end

现在 ChildCell 和 ChildCell2 获取 BaseCollectionViewCell 的所有方法。

将您的集合视图单元格注册到这些类以及单独的重用标识符。 使用以下代码使单元格出列。

 BaseCollectionViewCell *cell;

  if (condition) {

  ChildCell *cell1 = (ChildCell *)[collectionView      
  dequeueReusableCellWithReuseIdentifier:kCustomCell1Identifier 
  forIndexPath:indexPath];
  //call cell1's specific properties if needed.
  cell = (BaseCollectionViewCell *) cell1;

  } else {

   ChildCell2 cell2 = (ChildCell2 *)[collectionView 
   dequeueReusableCellWithReuseIdentifier:kCustomCell2Identifier forIndexPath:indexPath];
  //call cell2's specific properties if needed.
  cell = (BaseCollectionViewCell *) cell2;

  }
//call common methods here.

【讨论】:

  • 您能否还补充一下,我在问题中所写的 cellForItemAtIndexPath 方法中的代码将如何?
  • BaseCollectionViewCell *cell;有了它,是的,您可以访问两个单元格中的公共属性,但是其他的呢,例如 childCell1 有一个名为 titleLabel 的标签,因此 cell.titleLabel 现在无法访问,因为它不是`BaseCollectionViewCell`的一部分希望你明白我的意思跨度>
  • @MikeAlterwe 可以转换单元格以获取单元格的特定属性。我已添加评论。
猜你喜欢
  • 2012-05-17
  • 1970-01-01
  • 2013-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-28
相关资源
最近更新 更多