gpt4 book ai didi

ios - CollectionView多单元格选择

转载 作者:搜寻专家 更新时间:2023-10-31 08:13:04 27 4
gpt4 key购买 nike

我有一个包含两个自定义单元格的 Collection View ,一个用于网格,一个用于列表,我希望能够触摸单元格并选择它们,就好像要删除或共享它们一样,我现在想要的只是能够选择和取消选择它们,我会在下面发布我的代码,结果是当我触摸一个单元格时,所有单元格都被选中!这是代码:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if isGridSelected {

let cell:cell2_Class = collectionView.dequeueReusableCellWithReuseIdentifier("cell2", forIndexPath: indexPath) as! cell2_Class

cell.listImage.image = imageArray[indexPath.row]

if flag == true {
cell.layer.borderColor = UIColor.blueColor().CGColor
cell.layer.borderWidth = 3
cancelButton.hidden = false
} else {
cell.layer.borderColor = UIColor.clearColor().CGColor
cancelButton.hidden = true
}
return cell
} else {
let cell:PhotoCollectionCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! PhotoCollectionCell

if flag == true {
cell.layer.borderColor = UIColor.blueColor().CGColor
cell.layer.borderWidth = 3
cancelButton.hidden = false
} else {
cell.layer.borderColor = UIColor.clearColor().CGColor
cancelButton.hidden = true
}
cell.imageView.image = imageArray[indexPath.row]
cell.NameLabel.text = namelabel[indexPath.row]
cell.ModifiedLbl.text = modfLabel[indexPath.row]

return cell
}
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

let cell = collectionView.cellForItemAtIndexPath(indexPath)

if cell!.selected == true {
flag = true
} else {
flag = false
}
self.collectionView.reloadData()
}

最佳答案

PhotoCollectionCellcell2_Class 中(或在普通的 superclass 中)简单地覆盖这个方法

- (void) setSelected:(BOOL)selected 
{
if (selected)
{
self.layer.borderColor = UIColor.blueColor().CGColor
self.layer.borderWidth = 3
}
else
{
self.layer.borderColor = UIColor.clearColor().CGColor
}
}

那么您就不必在delegatedataSource 中处理实际的选择/突出显示

确保您的 collectionView 的属性 allowsSelectionYES

如果您想要多选,那么还要将allowsMultipleSelection 设置为YES 并在您的委托(delegate) 中实现以下方法

- (BOOL) collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
if ([collectionView.indexPathsForSelectedItems containsObject: indexPath])
{
[collectionView deselectItemAtIndexPath: indexPath animated: YES];
return NO;
}
return YES;
}

快速解决方案

collectionViewCell 的子类

override var selected: Bool {
didSet {
self.layer.borderWidth = 3.0
self.layer.borderColor = selected ? UIColor.blueColor().CGColor : UIColor.clearColor().CGColor
}
}

UICollectionViewDelegate 中:

func collectionView(collectionView: UICollectionView, shouldSelectItemAt indexPath: NSIndexPath) -> Bool {
if let selectedItems = collectionView.indexPathsForSelectedItems() {
if selectedItems.contains(indexPath) {
collectionView.deselectItemAtIndexPath(indexPath, animated: true)
return false
}
}
return true
}

关于ios - CollectionView多单元格选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39558482/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com