使用两个类似的集合视图单元,从而避免代码重复

时间:2015-04-14 13:35:54

标签: c# ios xamarin xamarin.ios uicollectionviewcell

我想要两个几乎相似的细胞。不同之处仅在于在其上再显示一个视图。

因此我想使用自定义构造函数。通常你有一个类似于这个的构造函数:

public class AnimalCell : UICollectionViewCell
{
    [Export ("initWithFrame:")]
    public AnimalCell (CGRect frame) : base (frame)
    {
        // do something
    }
}

我想传递一个类型,根据这种类型,我想在单元格上显示不同的项目。最好的方法是使用这样的构造函数:

public AnimalCell(MyCustomType type)
{
    if(type == XXX){
        // add as subview, add constraints, ...
    }else{
        // normal setup
    }
}

我想保持细胞重用当然。这可以实现吗?怎么样?

我想到的另一件事是使用子类化。有没有人知道我如何定义 cell 以便我不必复制相同的代码? E.g。

var cell = null;
if (MyCustomType == XXX) {
    cell = collectionView.DequeueReusableCell (DefaultCell.Key, indexPath) as DefaultCell;
} else {
    cell = collectionView.DequeueReusableCell (CustomizedCell.Key, indexPath) as CustomizedCell;
}
cell.DoSomething("someValue"); // this doesn't work because you have to define cell with a certain class
// do some more initialization

我目前的解决方案:

public abstract class DefaultCell : UICollectionViewCell
{
    protected bool someVariable;

    [Export ("initWithFrame:")]
    public DefaultCell (CGRect frame) : base (frame)
    {
    }

    public void DoSomething(string text)
    {
        label.Text = text;
    }
}

public class Custom1Cell : DefaultCell
{
    public static readonly NSString Key = new NSString ("Custom1Cell");

    [Export ("initWithFrame:")]
    public Custom1Cell (CGRect frame) : base (frame)
    {
        initialize ();
    }

    private void initialize()
    {
        // some initialization
    }
}

public class Custom2Cell : DefaultCell
{
    public static readonly NSString Key = new NSString ("Custom2Cell");

    [Export ("initWithFrame:")]
    public Custom2Cell (CGRect frame) : base (frame)
    {
        initialize ();
    }

    private void initialize()
    {
        // some initialization
    }

    public void SomeMethod(string text)
    {
        if(someVariable)
            someOtherLabel.Text = text;
    }
}

马哈茂德描述的出局工作:

DefaultCell cell;
if (type = XXX) {
    cell = collectionView.DequeueReusableCell (Custom1Cell.Key, indexPath) as Custom1Cell;
} else {
    cell = collectionView.DequeueReusableCell (Custom2Cell.Key, indexPath) as Custom2Cell;
}
cell.DoSomething("works on both cell types");
((Custom2Cell)cell).SomeMethod("works only on one cell type");

2 个答案:

答案 0 :(得分:3)

也许你会考虑一个带有共享变量和方法的抽象类(将它命名为BaseClass)和另外两个继承自抽象类的类,在代码中你可以将cell定义为Object并在if语句中将其初始化,如下所示:

Object cell;
if (MyCustomType == XXX) {
    cell = new class1()
   ((Class1) cell).MethodInClass1();
} else{
   cell = new class2();
   ((Class2) cell).MethodInClass2();
}

答案 1 :(得分:1)

您可以添加一个UICollectionViewCell的子类,让我们说是YourCell。现在将YourCell提供给故事板中的单元格。在YourCell中,您可以添加任意数量的视图,并且可以将它们从故事板单元格插入到YourCell类中,然后您可以根据需要自定义视图。

<强> Updat: 好吧,假设你没有在故事板中添加该视图,但是你将它作为一个属性(我只是一个指针,所以不会占用内存:)很多)只需在getter上进行惰性实例化并设置它即可。这个getter方法的约束。这是一个懒惰实例化的例子,我们有一个不在故事板视图中的标签。

-(UILabel *)yourLabel
{
    if(!_yourLabel){
        _yourLabel=[[UILabel alloc]init];
        NSLayoutConstraint *leadingConstraint= [NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:_yourLabel attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0];
         NSLayoutConstraint *topConstraint= [NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_yourLabel attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
        [self.contentView addConstraints:@[leadingConstraint,topConstraint]];
    }
    return _yourLabel;
}