[英]CollectionView multiple cell selection
我有一个集合视图,有两个自定义单元格,一个用于网格,一个用于列表,我希望能够触摸单元格并选择它们,如果要删除或共享它们,我现在想要的只能选择和deselct他们,生病我的代码下面的结果是当我触摸一个单元格所有单元格被选中! 这是代码:
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()
}
在PhotoCollectionCell
和cell2_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
}
}
然后,您不必处理delegate
或dataSource
的实际selection/highlighting
。
确保将collectionView
的属性allowsSelection
为YES
。
如果您想要multiple selection
那么还要将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
}
基于Aerows解决方案Swift 4.2
collectionViewCell
子类
override var isSelected: Bool {
didSet {
self.layer.borderWidth = 3.0
self.layer.borderColor = isSelected ? UIColor.blue.cgColor : UIColor.clear.cgColor
}
}
在UICollectionViewDelegate
func collectionView(_ collectionView: UICollectionView, shouldDeselectItemAt indexPath: IndexPath) -> Bool {
if let selectedItems = collectionView.indexPathsForSelectedItems {
if selectedItems.contains(indexPath) {
collectionView.deselectItem(at: indexPath, animated: true)
return false
}
}
return true
}
而且非常重要的是,在viewDidLoad()
不要忘记允许你的collectionView多项选择
collectionView.allowsMultipleSelection = true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.