2016-11-27 4 views
0

プロトタイプセルにテーブルとコレクションビューを含むView Controllerがあります。私は表のためのデータソースとデリゲートの両方としてVCを設定しているが、私の質問は、私はcollectionViewのデータソースとデリゲートとして設定しないものですデータソースとして何を設定し、テーブル内のcollectionViewの代理を行うか

enter image description here

私は単に数をハードコーディングするとき私のコードは正常に動作しますコレクションビューのセクションなどのしかし、私はplistファイルから配列を読み込むしようとすると、何かがうまくいかないと、同じ画像が各セクションここ enter image description here

のために表示されtableViewCell

import UIKit 

class CategoryRow: UITableViewCell { 
var pictDataSource = PicDataSource() 

} 

// Mark: Set-up the CollectionView 

extension CategoryRow: UICollectionViewDataSource { 

func numberOfSections(in collectionView: UICollectionView) -> Int { 

    return pictDataSource.numberOfSections 
} 

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return pictDataSource.numberOfPicsInSection(section) 
    // return 12 
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "videoCell", for: indexPath) as! CategoryRowCollectionViewCell 
    if let picc = pictDataSource.picForItemAtIndexPath(indexPath) { 
    cell.pic = picc 
    } 
    configreCell(cell, atIndexPath: indexPath) 
    return cell 
} 

    func configreCell(_ cell: CategoryRowCollectionViewCell, atIndexPath indexPath: IndexPath) { 
     cell.cellLabel.text = "\(indexPath.row)" 
    } 
} 

// Mark: Create the CollectionView flow layout 

extension CategoryRow: UICollectionViewDelegateFlowLayout { 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
     let itemsPerRow: CGFloat = 4 
     let hardCodedPadding: CGFloat = 5 
     let itemWidth = (collectionView.bounds.width/itemsPerRow) - hardCodedPadding 
     let itemHeight = collectionView.bounds.height - (2 * hardCodedPadding) 
     return CGSize(width: itemWidth, height: itemHeight) 
    } 

} 

ためのコードであり、これは読むためのコードでありますデータソースから

// 
// PicDataSource.swift 

import Foundation 
import UIKit 

class Pict { 
var caption: String 
var imageName: String 
var section: String 
var index: Int 

init(caption: String, imageName: String, section: String, index: Int) { 
    self.caption = caption 
    self.imageName = imageName 
    self.section = section 
    self.index = index 
} 
    convenience init(copying pic: Pict) { 
     self.init(caption: pic.caption, imageName: pic.imageName, section: pic.section, index: pic.index) 
    } 
} 

class PicDataSource { 

var pics: [Pict] = [] 
var sections: [String] = [] 
var immutablePics: [Pict] = [] 

var count: Int { 
    return pics.count 
} 

var numberOfSections: Int { 
    return sections.count 
} 

init() { 
    pics = loadImagesFromDisk() 
    immutablePics = pics 
} 


func loadImagesFromDisk() -> [Pict] { 
    sections.removeAll(keepingCapacity: false) 
    if let path = Bundle.main.path(forResource: "Images", ofType: "plist") { 
    if let dictArray = NSArray(contentsOfFile: path) { 
     // print ("DICTARRAY COUNT", dictArray.count) 
     var pics: [Pict] = [] 
     for item in dictArray { 
      if let dict = item as? NSDictionary { 

       let caption = dict["caption"] as! String 
       let imageName = dict["imageName"] as! String 
       let section = dict["section"] as! String 
       let index = dict["index"] as! Int 

       let pic = Pict(caption: caption, imageName: imageName, section: section, index: index) 
       if !sections.contains(section) { 
       sections.append(section) 
       } 
       pics.append(pic) 
      } 
     } 

     return pics.sorted { $0.section < $1.section } 

    } 
    } 
    return [] 
} 

func numberOfPicsInSection(_ index: Int) -> Int { 
    let pics = picsForSection(index) 
    return pics.count 
} 

func picForItemAtIndexPath(_ indexPath: IndexPath) -> Pict? { 
    if indexPath.section > 0 { 
    let picss = picsForSection(indexPath.section) 
    return picss[indexPath.item] 
    } else { 
    return pics[indexPath.item] 
    } 
} 

func titleForSectionAtIndexPath(_ indexPath: IndexPath) -> String? { 
    if indexPath.section < sections.count { 
    return sections[indexPath.section] 
    } 
    return nil 
} 


func picsForSection(_ index: Int) -> [Pict] { 
    let section = sections[index] 
    let picsInSection = pics.filter { (pic: Pict) -> Bool in 
     return pic.section == section 
    } 
     return picsInSection 
    } 

} 

のViewController

import UIKit 

class ViewController: UIViewController, UITableViewDataSource { 

// var categories = ["Action", "Drama", "Science Fiction", "Kids", "Horror"] 
var categories = PicDataSource() 

override func viewDidLoad() { 
    super.viewDidLoad() 
     } 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

func numberOfSections(in tableView: UITableView) -> Int { 

    return categories.numberOfSections 
} 

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 

    return categories.sections[section] 

} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 1 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CategoryRow 
    return cell 
    } 


} 

誰もが私が間違ってやっているものに、任意のアイデアやポインタを持っている場合、私はgratefullこと

答えて

1

あなたがUICollectionViewDataSourceUICollectionViewDelegateメソッドを実装しますどんなクラスにコレクションビューのdataSourcedelegateを設定します。テーブルビューのdataSourcedelegateと同じビューコントローラが使用されますが、必ずしもそうである必要はありません。

関連する問題