简体   繁体   English

如何在展开的UICollectionView行内添加UITableView?

[英]How to add UITableView inside expanded UICollectionView row?

There are two basic functionalities I want to attach to my collection view: 我要附加到收藏夹视图的两个基本功能:

  1. The cells show expand and collapse behaviour on didSelectItem method. 单元格在didSelectItem方法上显示展开和折叠行为。
  2. Cell which gets expanded show a new tableView relevant to the row that was selected. 展开的单元格将显示一个与所选行相关的新tableView。

Presumptions : 推定

  1. Multiple expansions don't take place. 不会进行多次扩展。
  2. Upon clicking any unexpanded cell,that particular cell should expand and rest all should collapse. 单击任何未展开的单元格后,该特定的单元格应展开,其余所有单元应折叠。
  3. The click of the cells in collection view and internal UITableView inside each cell has to be handled. 必须处理集合视图中的单元格单击以及每个单元格内部的内部UITableView。
  4. Each row of the UICollectionView can attain different height with respect to the size of the UITableView it will load upon click it. 关于UICollectionView的每一行,在单击时将加载的UITableView的大小可以达到不同的高度。

I tried How to expand collectionview cell on didselect to show more info? 我尝试过如何在didselect上展开collectionview单元以显示更多信息? , but it does not call cellForItemAtIndexPath, and so I am unable to add the new tableView to the cell, or http://www.4byte.cn/question/465532/how-to-expand-collectionview-cell-on-didselect-to-show-more-info.html while Animate UICollectionViewCell collapsing is still unanswered. ,但它不会调用cellForItemAtIndexPath,因此我无法将新的tableView添加到单元格,或http://www.4byte.cn/question/465532/how-to-expand-collectionview-cell-on-didselect -to-show-more-info.html,Animate UICollectionViewCell崩溃仍未得到解决。

I have a custom cell for this collectionView. 我有一个用于此collectionView的自定义单元格。

Any help in this regard is appreciated. 在这方面的任何帮助表示赞赏。 Thanks in advance. 提前致谢。

Here is a sample, hope it helps to understand, how to implement. 这是一个示例,希望有助于理解如何实现。

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

Do not forget set dataSource and delegate in storyboard. 不要忘记在故事板中设置数据源和委托。

Result: 结果: 在此处输入图片说明

EDIT 编辑

    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()
            }
        }
    }

And update configureWithDictionary() method: 并更新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()
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM