栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

展开式UICollectionViewCell

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

展开式UICollectionViewCell

如果您有多个单元格需要在集合视图单元格中触摸按钮时扩展到不同的高度,那么这里是代码。

我使用委托模式来让控制器知道使用indexPath触摸了哪个单元格的按钮。创建单元时,需要将单元的索引路径传递到该单元。

当触摸按钮时,单元格将调用委托(ViewController),该代理程序将相应地更新isExpandedArray并重新加载特定的单元格。

CollectionViewCell

 protocol ExpandedCellDelegate:NSObjectProtocol{    func topButtonTouched(indexPath:IndexPath)}class ExpandableCell: UICollectionViewCell {    @IBOutlet weak var topButton: UIButton!    weak var delegate:ExpandedCellDelegate?    public var indexPath:IndexPath!    @IBAction func topButtonTouched(_ sender: UIButton) {        if let delegate = self.delegate{ delegate.topButtonTouched(indexPath: indexPath)        }    }}

查看控制器类

class ViewController: UIViewController {    @IBOutlet weak var collectionView: UICollectionView!    var expandedCellIdentifier = "ExpandableCell"    var cellWidth:CGFloat{        return collectionView.frame.size.width    }     var expandedHeight : CGFloat = 200     var notExpandedHeight : CGFloat = 50     var dataSource = ["data0","data1","data2","data3","data4"]     var isExpanded = [Bool]()    override func viewDidLoad() {        super.viewDidLoad()        isExpanded = Array(repeating: false, count: dataSource.count)    }}extension ViewController:UICollectionViewDataSource{    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {        return dataSource.count    }    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {          let cell = collectionView.dequeueReusableCell(withReuseIdentifier: expandedCellIdentifier, for: indexPath) as! ExpandableCell cell.indexPath = indexPath cell.delegate = self //configure Cell return cell    }}extension ViewController:UICollectionViewDelegateFlowLayout{    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {        if isExpanded[indexPath.row] == true{  return CGSize(width: cellWidth, height: expandedHeight)        }else{ return CGSize(width: cellWidth, height: notExpandedHeight)        }    }}extension ViewController:ExpandedCellDelegate{    func topButtonTouched(indexPath: IndexPath) {        isExpanded[indexPath.row] = !isExpanded[indexPath.row]        UIView.animate(withDuration: 0.8, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.9, options: UIViewAnimationOptions.curveEaseInOut, animations: {   self.collectionView.reloadItems(at: [indexPath]) }, completion: { success in     print("success")        })    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/369052.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号