ios 如何突出显示选定的 UICollectionView 单元格?(迅速)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30598664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I highlight selected UICollectionView cells? (Swift)
提问by bkSwifty
I have a UICollectionView, and the user is able to select multiple cells. It's a bit difficult to keep track of which cells have been selected, so I need some way to go about highlighting/creating a border when the cell is tapped.
我有一个 UICollectionView,用户可以选择多个单元格。跟踪选择了哪些单元格有点困难,所以我需要一些方法来在单元格被点击时突出显示/创建边框。
Code:
代码:
func collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool {
addToList.append(objectsArray[indexPath.row])
return true
}
回答by ?zgür Ersil
you can use border change on didSelectItemAtIndexPath
override event like the below code and assign new settings on the cell.
您可以didSelectItemAtIndexPath
像下面的代码一样在覆盖事件上使用边框更改并在单元格上分配新设置。
Swift 3.x:
斯威夫特 3.x:
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
addToList.append(objectsArray[indexPath.row])
let cell = collectionView.cellForItem(at: indexPath)
cell?.layer.borderWidth = 2.0
cell?.layer.borderColor = UIColor.gray.cgColor
}
回答by DawnSong
Here is my solution, and I'm sure it works.
这是我的解决方案,我确定它有效。
My solution includes 3 highlight effects, UICollectionCell
's selectedBackgroundView
, cell.contentView.backgroundColor
, or your your own specialHighlightedArea
; just feel free to choose the one you need, and feel free to add more effects as your App's Designer requires.
我的解决方案包括3种高亮效果,UICollectionCell
的selectedBackgroundView
,cell.contentView.backgroundColor
或你自己的specialHighlightedArea
; 只需随意选择您需要的一个,并根据您的应用程序设计器的需要随意添加更多效果。
How to use? Just inherit BaseCollectionViewCell
. If needed, configure in cell's init
or collectionView
's delegate methods.
如何使用?只是继承BaseCollectionViewCell
。如果需要,请在单元格的init
或collectionView
的委托方法中进行配置。
If you don't need highlight effect, just find a method named 'shouldHighlightItemAtIndexPath' in UICollectionViewDelegateand return false
or just set cell.shouldTintBackgroundWhenSelected = false
.
如果您不需要高亮效果,只需在UICollectionViewDelegate 中找到一个名为“ shouldHighlightItemAtIndexPath”的方法并返回false
或设置即可cell.shouldTintBackgroundWhenSelected = false
。
extension UIColor {
convenience init(rgb: Int, alpha: CGFloat = 1.0) {
self.init(red: CGFloat((rgb & 0xFF0000) >> 16) / 255.0, green: CGFloat((rgb & 0xFF00) >> 8) / 255.0, blue: CGFloat(rgb & 0xFF) / 255.0, alpha: alpha)
}
}
/// same with UITableViewCell's selected backgroundColor
private let cellHighlightedColor = UIColor(rgb: 0xD8D8D8)
class BaseCollectionViewCell: UICollectionViewCell {
var shouldTintBackgroundWhenSelected = true // You can change default value
var specialHighlightedArea: UIView?
// make lightgray background show immediately(on touch)
// (使灰背景在手指触到 cell 时立即出现)
override var isHighlighted: Bool {
willSet {
onSelected(newValue)
}
}
// keep lightGray background from selected until unselected
// (保留灰背景直至取消选中)
override var isSelected: Bool {
willSet {
onSelected(newValue)
}
}
func onSelected(_ newValue: Bool) {
// selectedBackgroundView is defined by UICollectionViewCell
guard selectedBackgroundView == nil else { return }
if shouldTintBackgroundWhenSelected {
contentView.backgroundColor = newValue ? cellHighlightedColor : UIColor.clear
}
if let sa = specialHighlightedArea {
sa.backgroundColor = newValue ? UIColor.black.withAlphaComponent(0.4) : UIColor.clear
}
}
}
回答by Sunkas
Use
用
collectionView.reloadItemsAtIndexPaths([indexPath])
to reload current cell, or
重新加载当前单元格,或
collectionView.reloadData()
to reload all cells in shouldSelectItemAtIndexPath
重新加载所有单元格 shouldSelectItemAtIndexPath
Then in cellForItemAtIndexPath
set your border or background color if the cell is marked as checked (you may need a new array for checked cells with preferably indexPaths.
然后,cellForItemAtIndexPath
如果单元格被标记为已选中,则设置边框或背景颜色(您可能需要一个新数组用于选中的单元格,最好使用 indexPaths.
回答by Strong
You can create a customized collcetionViewCell, and override:
您可以创建自定义的 colcetionViewCell,并覆盖:
class MyCell: UICollectionViewCell {
override var isHighlighted: Bool {
didSet {
if self.isHighlighted {
print("yes")
// Your customized animation or add a overlay view
} else {
print("no")
// Your customized animation or remove overlay view
}
}
}
}
This way, you can create similar result like the highlight effect on UITableViewCell.
这样,您可以创建类似于 UITableViewCell 上的高亮效果的结果。
Without subclassing:
没有子类化:
If you don't want to create your own collectionViewCell. you can use the delegate method:
如果您不想创建自己的 collectionViewCell。您可以使用委托方法:
func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath)
func collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath)
You can do the same thing with it.
你可以用它做同样的事情。
回答by drfalcoew
SWIFT
迅速
Add this code to your UICollectionViewCell subclass:
将此代码添加到您的 UICollectionViewCell 子类中:
override var isSelected: Bool {
didSet{
if self.isSelected {
self.backgroundColor = UIColor(red: 115, green: 190, blue: 170, alpha: 1.0)
}
else {
self.backgroundColor = UIColor(red: 60, green: 63, blue: 73, alpha: 1.0)
}
}
}
This will set the color of a single selected cell, and remove the selected color from any previous selected cells.
这将设置单个选定单元格的颜色,并从任何先前选定的单元格中删除选定的颜色。
回答by H S Progr
For multiple selection of cell, you can do it as follow:
对于多选单元格,您可以按如下方式进行:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
if let currentCell = collectionView.cellForItem(at: indexPath) as? QuestionnaireCollectionViewCell {
// Your selection logic, you can change it according to your requirement
if currentCell.selectedImage.isHidden == true{
currentCell.selectedImage.isHidden = false
}
else{
currentCell.selectedImage.isHidden = true
}
}
}
}
For single selection you can use isSelected in your collectionviewcell class as follow:
对于单个选择,您可以在 collectionviewcell 类中使用 isSelected ,如下所示:
override var isSelected: Bool{
didSet{
if self.isSelected
{
//This block will be executed whenever the cell's selection state is set to true (i.e For the selected cell)
}
else
{
//This block will be executed whenever the cell's selection state is set to false (i.e For the rest of the cells)
}
}
}
回答by Deepansh Jagga
Try to make the borders thick enough to cover the entire cell
尝试使边框足够厚以覆盖整个单元格
Code:
代码:
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
addToList.append(objectsArray[indexPath.row])
let cell = collectionView.cellForItem(at: indexPath)
cell?.layer.borderWidth = 200.0
cell?.layer.borderColor = UIColor.init(red: 0/255, green: 0/255, blue: 0/255, alpha: 0.4).cgColor
}