簡體   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