我想将两个基本功能附加到我的集合视图中:
推定:
我尝试How to expand collectionview cell on didselect to show more info?,但它没有调用cellForItemAtIndexPath,因此我无法将新的tableView添加到单元格,或http://www.4byte.cn/question/465532/how-to-expand-collectionview-cell-on-didselect-to-show-more-info.html 虽然Animate UICollectionViewCell collapsing仍未得到答复。
我有一个自定义单元格用于此collectionView。
对此方面的任何帮助表示赞赏。 提前谢谢。
答案 0 :(得分:0)
这是一个示例,希望它有助于理解,如何实现。
class WSCustomCollectionViewCell: UICollectionViewCell, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var tableView: UITableView!
var tableViewData: Array<String>?
// MARK: UITableviewDataSource
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("wsTableViewCell") as UITableViewCell!
var value = self.tableViewData![indexPath.row] as String
cell.textLabel?.text = value
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if self.tableViewData != nil {
return self.tableViewData!.count
}
return 0
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
// MARK: Configure Cell
func configureWithDictionary(value: Dictionary<String, AnyObject>) {
var title = value["name"] as? String
if title != nil {
self.titleLabel.text = title
}
var items = value["items"] as? Array<String>
if items != nil {
self.tableViewData = items
self.tableView.reloadData()
}
}
}
-
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet weak var collectionView: UICollectionView!
var collectionViewData: Array<Dictionary<String,AnyObject>>?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var value: Dictionary<String, AnyObject> = ["name":"Some title", "items":["item 1", "item 2"]]
self.collectionViewData = [value]
self.collectionView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: UICollectionViewDataSource
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if self.collectionViewData != nil {
return collectionViewData!.count
}
return 0
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("wsCell", forIndexPath: indexPath) as WSCustomCollectionViewCell
let value = self.collectionViewData![indexPath.row] as Dictionary<String, AnyObject>?
if value != nil {
cell.configureWithDictionary(value!)
}
return cell
}
}
不要忘记在storyboard中设置dataSource和delegate。
结果:
修改强>
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let value = self.collectionViewData![indexPath.row] as Dictionary<String, AnyObject>?
// WARNING: very dirty solution, only for understanding!
if value != nil {
var isExpand = value!["expand"] as? String
if isExpand != nil && isExpand == "1" {
return CGSizeMake(280, 200)
}
}
return CGSizeMake(280, 44)
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let value = self.collectionViewData![indexPath.row] as Dictionary<String, AnyObject>?
if value != nil {
// WARNING: very dirty solution, only for understanding!
var isExpand = value!["expand"] as? String
if isExpand != nil && isExpand == "0" {
var newValue: Dictionary<String, AnyObject> = ["name":"Some title", "items":["item 1", "item 2"], "expand":"1"]
self.collectionViewData = [newValue]
self.collectionView.reloadData()
}
else {
var newValue: Dictionary<String, AnyObject> = ["name":"Some title", "items":["item 1", "item 2"], "expand":"0"]
self.collectionViewData = [newValue]
self.collectionView.reloadData()
}
}
}
并更新configureWithDictionary()方法:
func configureWithDictionary(value: Dictionary<String, AnyObject>) {
var title = value["name"] as? String
if title != nil {
self.titleLabel.text = title
}
var items = value["items"] as? Array<String>
var isExpand = value["expand"] as? String
if items != nil && isExpand != nil && isExpand == "1" {
self.tableViewData = items
self.tableView.reloadData()
}
else {
self.tableViewData = nil
self.tableView.reloadData()
}
}