我有一个带有collectionview
分页的flowlayout
,并且在collectionviewCell
里面我添加了tableview
,所以现在的问题是,每当我滚动collectionview
时都挂起或抽搐,并且突然增加了内存使用量。
然后我从tableview
中删除collectionviewCell
,它运行良好,但是我的要求是将tableview
放在collectionCell
里面,那我该怎么办?
//MARK:- CollectionView Methods
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if section == 0 {
return arrRecent.count
}else {
return arrAnswered.count
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.section == 0 {
if indexPath.row == 1 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeRadioSelectionCell", for: indexPath) as! HomeRadioSelectionCell
cell.setupFields()
cell.btnOptionOne.leftImage(image: UIImage(named: "ic_checked_radio")!, renderMode: .alwaysOriginal, state: .selected)
cell.btnOptionOne.leftImage(image: UIImage(named: "ic_unchecked_radio")!, renderMode: .alwaysOriginal, state: .normal)
cell.btnOptiontwo.leftImage(image: UIImage(named: "ic_checked_radio")!, renderMode: .alwaysOriginal, state: .selected)
cell.btnOptiontwo.leftImage(image: UIImage(named: "ic_unchecked_radio")!, renderMode: .alwaysOriginal, state: .normal)
cell.btnOptionthree.leftImage(image: UIImage(named: "ic_checked_radio")!, renderMode: .alwaysOriginal, state: .selected)
cell.btnOptionthree.leftImage(image: UIImage(named: "ic_unchecked_radio")!, renderMode: .alwaysOriginal, state: .normal)
//
cell.btnOptionOne.addTarget(self, action: #selector(self.btnOptionOneClick(sender:)), for: .touchUpInside)
cell.btnOptiontwo.addTarget(self, action: #selector(self.btnOptionTwoClick(sender:)), for: .touchUpInside)
cell.btnOptionthree.addTarget(self, action: #selector(self.btnOptionThreeClick(sender:)), for: .touchUpInside)
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped))
cell.imgHeader.isUserInteractionEnabled = true
cell.imgHeader.addGestureRecognizer(tapGestureRecognizer)
return cell
}else if indexPath.row == 2 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeEmojiCollectionCell", for: indexPath) as! HomeEmojiCollectionCell
cell.setupFields()
cell.slider.addTarget(self, action: #selector(self.sliderValue(_sender:)), for: [.touchUpInside,.touchUpOutside,.touchCancel,.valueChanged])
return cell
}
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! HomeCollectionViewCell
cell.setupFields()
cell.btnBar.addTarget(self, action: #selector(self.barViewClick(_sender:)), for: .touchUpInside)
cell.btnCancel.addTarget(self, action: #selector(self.barCancelClick(_sender:)), for: .touchUpInside)
return cell
}else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AnsweredCollectionViewCell", for: indexPath) as! AnsweredCollectionViewCell
cell.setupChart()
cell.cardView.enableRippleBehavior = false
cell.cardView.inkView.inkColor = UIColor.clear
cell.cardView.layer.cornerRadius = 8
cell.cardView.clipsToBounds = true
return cell
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
if section == 0 {
return UIEdgeInsets.init(top: 0, left: 0, bottom: 5, right: 40)
}else if section == 1 {
return UIEdgeInsets.init(top: 0, left: 30, bottom: 5, right: 30)
}else {
return UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0)
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
var cellSize: CGSize = collectionView.bounds.size
cellSize.width -= collectionView.contentInset.right - 5
cellSize.width -= collectionView.contentInset.left - 5
cellSize.height -= collectionView.contentInset.bottom
return cellSize
}
HomeCollectionViewCell文件中的tableview方法
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tblView.dequeueReusableCell(withIdentifier: "HomeTableViewCell", for: indexPath) as! HomeTableViewCell
cell.btnOption.enableRippleBehavior = true
cell.btnOption.setElevation(ShadowElevation(rawValue: 2), for: .normal)
cell.btnOption.leftImage(image: UIImage(named: "ic_check")!, renderMode: .alwaysOriginal, state: .selected)
cell.btnOption.leftImage(image: UIImage(named: "ic_uncheck")!, renderMode: .alwaysOriginal, state: .normal)
cell.btnOption.addTarget(self, action: #selector(self.btnOptionClick(sender:)), for: .touchUpInside)
return cell
}
如果您想查看更多代码,请让我知道:)
先谢谢了:)