我将啥设置为表中的 collectionView 的数据源和委托
Posted
技术标签:
【中文标题】我将啥设置为表中的 collectionView 的数据源和委托【英文标题】:what do I set as the Datasource and delegate for a collectionView within a table我将什么设置为表中的 collectionView 的数据源和委托 【发布时间】:2016-11-27 18:02:45 【问题描述】:我有一个视图控制器,它在原型单元格中包含一个表格和一个集合视图。我已将 VC 设置为 Table 的 DataSource 和 Delegate,但我的问题是我将什么设置为 CollectionView 的 Datasource 和 delegate
当我简单地对集合视图的部分数量等进行硬编码时,我的代码可以正常工作。但是,当我尝试从 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
视图控制器
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】:您将集合视图的dataSource
和delegate
设置为将实现UICollectionViewDataSource
和UICollectionViewDelegate
方法的任何类。这很可能与您的表格视图的dataSource
和delegate
使用相同的视图控制器,但不一定是。
【讨论】:
以上是关于我将啥设置为表中的 collectionView 的数据源和委托的主要内容,如果未能解决你的问题,请参考以下文章
当 IDENTITY_INSERT 设置为 OFF 时,不能为表中的标识列插入显式值