简体   繁体   中英

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? 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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