UICollectionViewを使っています。
ユーザーの設定により表示させるCellを変更出来るような物を作りたいと思っています。
swift
1var contentArray = [] // ユーザーが使用するコンテンツの種類を入れる
の様に表示させるコンテンツを用意して(配列に何を入れるのがベストなのかもわからないです...)
Swift
1 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 2 let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! カスタムクラス名 3 cell.setContentData(data) 4 return cell 5 }
のような感じで実装できないかな〜?と思っているんですが、「カスタムクラス名」の部分をどうすればいいのかわからない状態です。
出来るだけ、コンテンツ数が増減した場合でも簡単に対応できるようにしたいのですが、どうすれば教えて頂けますか?
具体的には
・「基本コンテンツがA,B,C」のみの場合
A>B>C
・アプリ内課金で「D,E」を購入
A>B>C>D>E
・アプリ内設定で「A,D,E」のみの表示に設定
A>D>E
の様にしたいのです。
UICollectionViewで実装できれば、楽なのかな?と思っているのですが...
よろしくお願いします。
//////////////////
4月7日 追記
こういうのを考えたんですがどうでしょうか?
テストで試したのでTableViewですが...
swift
1class CellManager: NSObject { 2 3 enum CellType { 4 case Cell_1 5 case Cell_2 6 } 7 8 var createCellTypes: [CellType] = []; 9 10 func createCell(tableView: UITableView, indexPath: NSIndexPath) -> BaseTableCell? { 11 12 switch createCellTypes[indexPath.row] { 13 case .Cell_1: 14 return Cell_1.createCellForIdentifer(tableView, indexPath: indexPath) 15 default: 16 return Cell_2.createCellForIdentifer(tableView, indexPath: indexPath) 17 } 18 } 19} 20
という、Cellの生成等を司るクラスCellManagerを用意しました。
次に、カスタムCellのベースになるクラスBaseTableCellを用意しました。
swift
1 2class BaseTableCell: UITableViewCell { 3 4 var cellIdentifier:String { 5 get { 6 return "" 7 } 8 } 9 10 class func createCellForIdentifer(tableView: UITableView, indexPath:NSIndexPath) -> BaseTableCell? 11 { 12 return nil; 13 } 14 15 func setContentData(data:AnyObject){} 16 17 override func awakeFromNib() { 18 super.awakeFromNib() 19 // Initialization code 20 } 21 override func setSelected(selected: Bool, animated: Bool) { 22 super.setSelected(selected, animated: animated) 23 24 // Configure the view for the selected state 25 } 26}
BaseTableCellのサブクラスとして__Cell_1__を作ります。
swift
1import UIKit 2 3class Cell_1: BaseTableCell { 4 5 override var cellIdentifier: String { 6 get { 7 return "cell_1" 8 } 9 } 10 override class func createCellForIdentifer(tableView: UITableView, indexPath: NSIndexPath) -> BaseTableCell? { 11 let cell = tableView.dequeueReusableCellWithIdentifier("cell_1", forIndexPath: indexPath) as! Cell_1 12 return cell; 13 } 14 15 override func setContentData(data: AnyObject) { 16 print(self.cellIdentifier); 17 } 18} 19
こんな感じでCell_2クラスも作りました。
ViewControllerで
swift
1 2 3 let cellManager = CellManager(); 4 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 // Do any additional setup after loading the view, typically from a nib. 8 tableView.delegate = self 9 tableView.dataSource = self 10 11 cellManager.createCellTypes = [.Cell_1, .Cell_2, .Cell_2, .Cell_2, .Cell_1] 12 } 13 14 15 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 16 return 30 17 } 18 19 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 20 let cell = CellManager.createCell(tableView, indexPath: indexPath) 21 cell?.setContentData("test"); 22 return cell!; 23 }
こうすると、Cellの表示・非表示などがCellManagerをいじるだけでスッキリ簡単に出来るのかな?と思っております。
もし、ご意見がありましたらコメントして頂けますとありがたいです。
よろしくお願いします。
あなたの回答
tips
プレビュー