简体   繁体   English

iOS单元格填充设计模式

[英]ios cell filling design pattern

I have a collection cell which used in multiple screens and setting lots of data in it. 我有一个用于多个屏幕的收集单元,并在其中设置了很多数据。 Which approach is better, setting data in UIViewController or in UICollectionviewCell? 在UIViewController或UICollectionviewCell中设置数据哪种方法更好? I didn't see second much but i don't know how to find right design pattern for this. 我没有看到太多,但我不知道如何找到正确的设计模式。 As example: 例如:

First: 第一:

@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;
  }
}

second: 第二:

@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];
     ..
  }
}

Your cell (which is a view), should not be aware of your model. 您的单元格(这是一个视图)应该不知道您的模型。 If we'll take a look on your second approach: 如果我们来看看您的第二种方法:

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

In that case, you view and model are aware of each other. 在这种情况下,您的视图和模型是相互了解的。

My suggestion is - everything regarding the view itself (text size, color, font etc.) you should do in your UICollectionViewCell initialization, and everything regarding the data should be placed in your UIViewController. 我的建议是-您应该在UICollectionViewCell初始化中执行的与视图本身有关的所有操作(文本大小,颜色,字体等),而与数据相关的所有操作都应放置在UIViewController中。

Check: http://www.objc.io/issue-1/lighter-view-controllers.html http://en.wikipedia.org/wiki/Separation_of_concerns 检查: 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