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