ios细胞填充设计模式



我有一个集合单元格,它在多个屏幕中使用,并在其中设置了大量数据。在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.htmlhttp://en.wikipedia.org/wiki/Separation_of_concerns

最新更新