我将什么设置为数据源并委托表中的collectionView

时间:2016-11-27 18:02:46

标签: ios uiviewcontroller uicollectionview

我有一个View Controller,它包含原型单元格中的表和集合视图。我已将VC设置为表的DataSource和Delegate,但我的问题是我将如何设置为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)
    }

}

这是从dataSource读取的代码

//
//  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
    }


}

如果有人对我做错了什么有任何想法或指示我会感激不尽

1 个答案:

答案 0 :(得分:1)

您可以将集合视图的dataSourcedelegate设置为将实施UICollectionViewDataSourceUICollectionViewDelegate方法的任何类。它可能与您的表格视图dataSourcedelegate使用的视图控制器相同,但它不一定非。