enter image description hereenter image description here
我想在swift中的集合视图的单元格中用灰色做一个边框线
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
selectedIndexPath = indexPath
}
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var borderColor: CGColor! = UIColor.clear.cgColor
var borderWidth: CGFloat = 0
if indexPath == selectedIndexPath{
borderColor = UIColor.brown.cgColor
borderWidth = 1 //or whatever you please
}else{
borderColor = UIColor.clear.cgColor
borderWidth = 0
}
cell.layer.borderWidth = borderWidth //You can use your component
cell.layer.borderColor = borderColor
}
I tried this way.
1条答案
按热度按时间vcirk6k61#
尝试这种方法,在viewDidLoad中将collection isPagingEnabled设置为true
在cellForItemAt中将CONTENTVIEW边框颜色设置为clear,宽度设置为0:
现在,在didSelectItemAt中,将所有可见单元格重置为无边框,并仅选择边框:
现在可以在scrollViewDidScroll方法中检测页面更改并重置边框:
这就是结果