tableView嵌套collectionView

首先是自定義collectionView填充的tableViewCellide

import UIKit

// 定義一個collectionView,重寫初始化大小和佈局方法
class TrendsDetailZanCollectionView: UICollectionView {
    
    var indexPath: NSIndexPath!

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
}

// collectionView的cellIdentity
let collectionViewCellIdentifier = "zanCollectionCell"

class TrendsDetailZanCVTableViewCell: UITableViewCell {
    
    // tableViewCell中添加collectionView屬性
    var collectionView: TrendsDetailZanCollectionView!
    
    // 重寫初始化方法,將collectionView加入tableViewCell中
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        // 設置佈局
        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsetsMake(15, 20, 0, 20) //四周間距
        layout.minimumLineSpacing = 27 
        layout.itemSize = CGSizeMake(62, 62) //每個部分的size
        layout.scrollDirection = UICollectionViewScrollDirection.Vertical
        self.collectionView = TrendsDetailZanCollectionView(frame: CGRectZero, collectionViewLayout: layout) // 初始化collectionView
        self.collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: collectionViewCellIdentifier)
        self.collectionView.showsVerticalScrollIndicator = false
        self.collectionView.backgroundColor = UIColor.whiteColor()
        self.contentView.addSubview(self.collectionView) // 將collectionView加入tableView中
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    // 注意⚠️重寫子視圖佈局方法,將collectionView的大小設置爲和tableViewCell大小相同
    override func layoutSubviews() {
        super.layoutSubviews()
        let frame = self.contentView.bounds
        self.collectionView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height)
    }
    
    // 設置代理刷新數據, 記錄下當前collectionView在tableView中的row或者indexPath
    func setCollectionViewDataSourceDelegate(dataSourceDelegate delegate: protocol<UICollectionViewDelegate,UICollectionViewDataSource>, index: NSInteger) {
        self.collectionView.dataSource = delegate
        self.collectionView.delegate = delegate
        self.collectionView.tag = index
        self.collectionView.reloadData()
    }

    func setCollectionViewDataSourceDelegate(dataSourceDelegate delegate: protocol<UICollectionViewDelegate,UICollectionViewDataSource>, indexPath: NSIndexPath) {
        self.collectionView.dataSource = delegate
        self.collectionView.delegate = delegate
        self.collectionView.indexPath = indexPath
        self.collectionView.tag = indexPath.section
        self.collectionView.reloadData()
    }
}

使用:佈局

// tableView的cell,這裏的identity是tableView的cellIdentity
            let zanCellIdentifier = "zanTableViewCell"
                     tableView.registerClass(TrendsDetailZanCVTableViewCell.self, forCellReuseIdentifier: zanCellIdentifier)
            let zanCell: TrendsDetailZanCVTableViewCell?  = tableView.dequeueReusableCellWithIdentifier(zanCellIdentifier, forIndexPath: indexPath) as? TrendsDetailZanCVTableViewCell
            tableView.separatorStyle = .None
            return zanCell!
// 針對當前tableViewCell設置collectionView
 func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if !currentPinglunView {
            if indexPath.row != 0 {
                if let collectionCell: TrendsDetailZanCVTableViewCell = cell as? TrendsDetailZanCVTableViewCell {
                    collectionCell.setCollectionViewDataSourceDelegate(dataSourceDelegate: self, index: indexPath.row)
                    // 設置collectionView的內容偏移量
                    let index: NSInteger = collectionCell.collectionView.tag
                    // contentOffsetDictionary內容偏移量,在scrollView中有針對collectionView進行設置[由於垂直滑動,key爲collectionView,value是x的偏移量]
                    let value: AnyObject? = self.contentOffsetDictionary.valueForKey(index.description)
                    let horizontalOffset: CGFloat = CGFloat(value != nil ? value!.floatValue : 0)
                    collectionCell.collectionView.setContentOffset(CGPointMake(horizontalOffset, 0), animated: false)
                }
            }
        }
    }

寫collectionView的代理方法ui

    // 首先有collectionView的代理方法
    // MARK: - UICollectionView Data source and Delegate
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return zanUserIcons.count
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        // 注意⚠️這裏的cellIdentity和自定義的collectionViewCell的Identity同樣!
        let cell: UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("zanCollectionCell", forIndexPath: indexPath)
        cell.backgroundColor = UIColor.whiteColor()
        let zanUserIconImageView = UIImageView.init(frame: CGRectMake(10, 0, 44, 44))
        zanUserIconImageView.image = zanUserIcons[indexPath.item]
        let zanuserNameLabel = UILabel.init(frame: CGRectMake(0, 52, 62, 10))
        log.debug(cell.subviews.description)
        // cell的重用,防重複添加子視圖
        if cell.subviews.count <= 1 {
            zanuserNameLabel.font = UIFont.systemFontOfSize(10)
            zanuserNameLabel.text = zanuserNames[indexPath.item]
            zanuserNameLabel.textColor = UIColor(hexString: "a8a8a8")
            zanuserNameLabel.textAlignment = .Center
            zanuserNameLabel.font = UIFont.systemFontOfSize(10)
            cell.addSubview(zanUserIconImageView)
            cell.addSubview(zanuserNameLabel)
        }
        // FIXME: - collectionCell高度計算
        let indexP = NSIndexPath.init(forRow: 1, inSection: 0)
        let maY = cell.frame.maxY
        let maX = cell.frame.maxX
        let x = SCREEN_WIDTH - maX
        // 當前子視圖在屏幕的最右時增長高度
        if maY >= currentCollectionHeight ||  x < 62 {
            currentCollectionHeight = currentCollectionHeight + maY
            if let cell = detailTableView.cellForRowAtIndexPath(indexP) {
                cell.bounds.size.height = currentCollectionHeight
                detailTableView.reloadRowsAtIndexPaths([indexP], withRowAnimation: UITableViewRowAnimation.Automatic)
            }
        }
        return cell
    }
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        log.debug("第\(collectionView.tag)行,第\(indexPath.item)個元素")
    }
     // tableView和collectionView代理都繼承scrollView代理,滑動觸發
    func scrollViewDidScroll(scrollView: UIScrollView) {
        if !scrollView.isKindOfClass(UICollectionView) {
            return
        }
        let horizontalOffset: CGFloat = scrollView.contentOffset.x
        let collectionView: UICollectionView = scrollView as! UICollectionView
        self.contentOffsetDictionary.setValue(horizontalOffset, forKey: collectionView.tag.description)
    }
相關文章
相關標籤/搜索