美文网首页Swift
iOS-swift3.0 点滴积累:TableViewCell嵌

iOS-swift3.0 点滴积累:TableViewCell嵌

作者: xiaopavip | 来源:发表于2017-07-01 16:00 被阅读0次

很多情况下,都会用到tableViewCell嵌套一个CollectionView,方便数据的展示与灵活控制,下面上代码来实现它。

新建一个TableViewCell并勾选创建xib。然后在xib中拖一个CollectionView并设置好约束。然后创建一个CollectionViewCell并勾选创建xib,并处理好要展示的CollectionViewCell的布局。

TableViewCell中代码:

import UIKit

class TableViewCell: UITableViewCell {
    static let identifier = "TableViewCell"
    //CollectionView数据源
    public var bankArray: [Model] = []
    // CollectionViewCell点击
    var gotoViewControllerCardClosure: ((_ bankId: Int)->Void)?
    
    @IBOutlet weak var collectionView: UICollectionView!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.itemSize = CGSize(width: (PCScreenWidth - 30) / 3, height: (PCScreenWidth - 30) / 3)
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        collectionView.collectionViewLayout = flowLayout
        collectionView!.register(UINib(nibName:"CollectionViewCell", bundle:nil),
                                      forCellWithReuseIdentifier: "cell")
        collectionView.delegate = self
        collectionView.dataSource = self
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}

extension TableViewCell: UICollectionViewDelegate, UICollectionViewDataSource {

    func collectionView(_ collectionView: UICollectionView,
                        numberOfItemsInSection section: Int) -> Int {
        return bankArray.count > 6 ? 6 : bankArray.count //最多显示6个CollectionViewCell,根据需要自己调整
    }
    
    func collectionView(_ collectionView: UICollectionView,
                        cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell  = collectionView.dequeueReusableCell(withReuseIdentifier: "cell",
                                                       for: indexPath) as! CreditCardCollectionViewCell
        let model = bankArray[indexPath.row]
        //设置CollectionViewCell  ...
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
        gotoViewControllerCardClosure?(bankArray[indexPath.row].id)
    }
}

ViewController中设置TableView的cell

let cell = tableView.dequeueReusableCell(withIdentifier: TableViewCell.identifier) as! TableViewCell
                    cell.bankArray = bankArray
                    cell. gotoViewControllerClosure = { [weak self] bankId in
                        guard let strongSelf = self else {
                            return
                        }
                        //跳转需要处理的业务
                        //.......
                    }
                    return cell

OK,大功告成~ 基本思路就是这样。如果展示效果不是自己想要的,再调整下对应设置与代码控制。

相关文章

  • iOS-swift3.0 点滴积累:TableViewCell嵌

    很多情况下,都会用到tableViewCell嵌套一个CollectionView,方便数据的展示与灵活控制,下面...

  • iOS-swift3.0 点滴积累:添加手势

    项目中很多时候,都需要用到添加手势。发个 两种方法都可以,方法1需要

  • iOS-swift3.0 点滴积累:设置控件指定角为圆角

    项目中用到只设置label的左下角和右下角为圆角,用UIBezierPath实现,记录一下。 其中,corner是...

  • iOS-swift3.0 点滴积累:身份证号码校验

    身份证号码校验,server端和app端都可以实现。下面实现swift3.0 校验身份证号码是否合法。用swfit...

  • 点滴积累

    2018-5-12 晴天 东莞 昨天回忆着自己和闺蜜的一路成长,自己挺多感悟的 心态决定行为,行为决定结果 ...

  • 点滴积累

    马上奔三了 技术之路有点恐慌 点滴积累 理论加实践

  • 点滴积累

    第一周 有人说,一个人忙起来,世界都是你的。 让自己忙起来,忙着工作,让界面一遍遍趋于精准;练习绘画,给自己定一个...

  • 点滴积累

    演示是参与者给予演讲者的一种恩惠! 合之成其大,必先分之而致其精!

  • 点滴积累

    你要每天不间断的去做对你的未来意义重大的事情。你为此花费的时间不会超过10分钟,但就是这10分钟会让一切变的...

  • 点滴积累

    改变 不论怎样出场,朝着目标前进。 凌晨1:20分睡觉时是有点疲惫,但不是特别累。6:00起床前,家人建议要不就休...

网友评论

    本文标题:iOS-swift3.0 点滴积累:TableViewCell嵌

    本文链接:https://www.haomeiwen.com/subject/tysbcxtx.html