繁体   English   中英

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

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

我要附加到收藏夹视图的两个基本功能:

  1. 单元格在didSelectItem方法上显示展开和折叠行为。
  2. 展开的单元格将显示一个与所选行相关的新tableView。

推定

  1. 不会进行多次扩展。
  2. 单击任何未展开的单元格后,该特定的单元格应展开,其余所有单元应折叠。
  3. 必须处理集合视图中的单元格单击以及每个单元格内部的内部UITableView。
  4. 关于UICollectionView的每一行,在单击时将加载的UITableView的大小可以达到不同的高度。

我尝试过如何在didselect上展开collectionview单元以显示更多信息? ,但它不会调用cellForItemAtIndexPath,因此我无法将新的tableView添加到单元格,或http://www.4byte.cn/question/465532/how-to-expand-collectionview-cell-on-didselect -to-show-more-info.html,Animate UICollectionViewCell崩溃仍未得到解决。

我有一个用于此collectionView的自定义单元格。

在这方面的任何帮助表示赞赏。 提前致谢。

这是一个示例,希望有助于理解如何实现。

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

不要忘记在故事板中设置数据源和委托。

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

编辑

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

暂无
暂无

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

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