繁体   English   中英

iOS单元格填充设计模式

[英]ios cell filling design pattern

我有一个用于多个屏幕的收集单元,并在其中设置了很多数据。 在UIViewController或UICollectionviewCell中设置数据哪种方法更好? 我没有看到太多,但我不知道如何找到正确的设计模式。 例如:

第一:

@implementation ProductViewController:UIviewController
{
  -(UICollectionviewCell*)CellForIndexpath:(UIcollectionview*) collectionview..{
      myCell *cell=[collectionview dequecellwithcellidentifier:@"cell"];
      Product *pr=[datasource objectAtIndex:indexpath.row];
      cell.lblName.text=pr.name;
      cell.lblSize.text=pr.size;
      [cell.imgCover setimage:pr.image];
      ..
      return cell;
  }
}

第二:

@implementation ProductViewController:UIviewController
{
  -(UICollectionviewCell*)CellForIndexpath:(UIcollectionview*) collectionview..{
      myCell *cell=[collectionview dequecellwithcellidentifier:@"cell"];
      Product *pr=[datasource objectAtIndex:indexpath.row];
      [cell initProduct:pr];
      return cell;
  }
}
@implemeantation myCell:UICollectionviewCell{
  -(void)initProduct:(Product*)pr{
     self.lblName.text=pr.name;
     self.lblSize.text=pr.size;
     [self.imgCover setimage:pr.image];
     ..
  }
}

您的单元格(这是一个视图)应该不知道您的模型。 如果我们来看看您的第二种方法:

   @implemeantation myCell:UICollectionviewCell{
    -(void)initProduct:(Product*)pr{
       self.lblName.text=pr.name;
       self.lblSize.text=pr.size;
       [self.imgCover setimage:pr.image];
       ..
    }
   }

在这种情况下,您的视图和模型是相互了解的。

我的建议是-您应该在UICollectionViewCell初始化中执行的与视图本身有关的所有操作(文本大小,颜色,字体等),而与数据相关的所有操作都应放置在UIViewController中。

检查: http : //www.objc.io/issue-1/lighter-view-controllers.html http://en.wikipedia.org/wiki/Separation_of_concerns

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM