下午好
我试图在UICollectionViewController中创建可扩展/可折叠单元。细胞正确膨胀和塌陷。但是,每个单元格所保存的信息仍保留在后台屏幕上。我将显示图片进行解释。如果用户折叠单元格,我试图从视图中删除信息,如果用户展开该单元格,则将其重新插入。
下面的图像表示细胞以折叠状态开始的过程,使该细胞膨胀,然后再次使该细胞塌陷。 (请注意:这不仅发生在我单击的第一个单元格上,而且还发生在每个单元格上。
第一张图片是在视图加载并且单元格处于原始折叠状态时。
第二张图片显示了用户点击单元格以展开它时的情况。
第三张图片显示了用户何时折叠单元格。
我用来展开和折叠单元格的代码在
下fileprivate let cellId = "cellId"
fileprivate let headerId = "headerId"
fileprivate let profID = "profID"
private let headerIdentifier = "userProfileHeader"
private let sectionIdentifier = "sectionHeader"
var section:Int?
var expandSection = [Bool]()
var items = [String]()
fileprivate func setupCollectionView() {
collectionView.backgroundColor = .white
collectionView.contentInsetadjustmentBehavior = .never
collectionView.register(UICollectionViewCell.self,forCellWithReuseIdentifier: cellId)
self.expandSection = [Bool](repeating: false,count: self.items.count)
collectionView.dataSource = self
collectionView.delegate = self
collectionView.register(userProfileHeader.self,forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader,withReuseIdentifier: headerIdentifier)
collectionView.register(sectionHeader.self,withReuseIdentifier: sectionIdentifier)
}
fileprivate func registerCollectionView() {
collectionView.register(profCell.self,forCellWithReuseIdentifier: "profCell")
self.collectionView.register(userProfileHeader.self,withReuseIdentifier: headerIdentifier)
self.collectionView.register(sectionHeader.self,withReuseIdentifier: sectionIdentifier)
}
var headerView: userProfileHeader?
var sectionView: sectionHeader?
override func collectionView(_ collectionView: UICollectionView,viewForSupplementaryElementOfKind kind: String,at indexPath: IndexPath) -> UICollectionReusableView {
if kind == UICollectionView.elementKindSectionHeader {
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind,withReuseIdentifier: headerIdentifier,for: indexPath) as? userProfileHeader
if let user = self.user {
headerView?.myUser = user
} else if let userToLoad = self.userToLoad {
headerView?.myUser = userToLoad
}
return headerView!
} else {
let sectionViews = collectionView.dequeueReusableSupplementaryView(ofKind: kind,withReuseIdentifier: sectionIdentifier,for: indexPath) as? sectionHeader
let categories = ["Skills,Preferences","Bio","Reviews"]
sectionViews!.headerLabel.text = categories[indexPath.section]
sectionViews!.backgroundColor = .lightGray
return sectionViews!
}
}
func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,referenceSizeForHeaderInSection section: Int) -> CGSize {
return .init(width: view.frame.width,height: 340)
}
override func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
return items.count
}
override func collectionView(_ collectionView: UICollectionView,didSelectItemAt indexPath: IndexPath) {
self.expandSection[indexPath.row] = !self.expandSection[indexPath.row]
self.collectionView.reloadItems(at: collectionView.indexPathsForSelectedItems!)
}
override func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "profCell",for: indexPath) as! profCell
if indexPath.row == 0 {
cell.educationView.text = user.education
cell.skillsView.text = user.skills
cell.preferencesView.text = "wassup bitches"
} else if indexPath.row == 1 {
cell.bioLabel.text = user.bio
} else if indexPath.row == 2 {
cell.labels.text = "Reviews"
}
}
return cell
}
func collectionView(_ collectionView: UICollectionView,sizeForItemAt indexPath: IndexPath) -> CGSize {
if self.expandSection[indexPath.row] {
return CGSize(width: self.view.bounds.width - 20,height: 300)
}else{
return CGSize(width: self.view.bounds.width - 20,height: 80)
}
}