簡體   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