问题描述
我已经使用代码在 UITableViewCell 内创建了一个 UICollectionView,我的目的是为每个图块(为每个 CollectionViewCell)创建一个卡片视图样式,但我没有正确获得卡片视图效果。到目前为止,这就是我所做的。
我的 CollectionViewCell 类如下
class CollectionViewCell: UICollectionViewCell {
let padding: CGFloat = 0
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit(){
backgroundColor = .white
self.contentView.backgroundColor = .white
contentView.addSubview(label)
updateConstraints()
}
override func updateConstraints() {
label.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
label.topAnchor.constraint(equalTo: topAnchor,constant: 0),label.leadingAnchor.constraint(equalTo: leadingAnchor,constant: 10),label.trailingAnchor.constraint(equalTo: trailingAnchor,label.bottomAnchor.constraint(equalTo: bottomAnchor,constant: 0)
])
super.updateConstraints()
}
public let label: UILabel = {
let v = UILabel()
v.textColor = .darkText
v.minimumScaleFactor = 0.5
v.numberOfLines = 1
return v
}()
}
和 MyTableViewCell 类如下
class ReposTableViewCell: UITableViewCell {
public lazy var collectionView: UICollectionView = { [uNowned self] in
let layout = UICollectionViewFlowLayout()
layout.minimumLinespacing = 10
layout.minimumInteritemSpacing = 0
layout.scrollDirection = .horizontal
let collectionView = UICollectionView(frame: .zero,collectionViewLayout: layout)
collectionView.register(CollectionViewCell.self,forCellWithReuseIdentifier: "CollectionCell")
collectionView.isScrollEnabled = true
collectionView.showsHorizontalScrollIndicator = false
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.backgroundColor = .white
return collectionView
}()
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
override init(style: UITableViewCell.CellStyle,reuseIdentifier: String?) {
super.init(style: style,reuseIdentifier: reuseIdentifier)
commonInit()
}
func commonInit(){
contentView.addSubview(collectionView)
updateConstraints()
}
func setCollectionViewDataSourceDelegate(dataSourceDelegate: UICollectionViewDataSource & UICollectionViewDelegate,forSection section: Int) {
collectionView.delegate = dataSourceDelegate
collectionView.dataSource = dataSourceDelegate
collectionView.tag = section
collectionView.reloadData()
}
override func sizeThatFits(_ size: CGSize) -> CGSize {
let collectionCellHeight = 50
let rows = 5 // 5: items per row
let height = CGFloat(collectionCellHeight * rows)
let width = contentView.frame.size.width
return CGSize(width: width,height: height)
}
override func updateConstraints() {
collectionView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
collectionView.topAnchor.constraint(equalTo: topAnchor),collectionView.leadingAnchor.constraint(equalTo: leadingAnchor),collectionView.bottomAnchor.constraint(equalTo: bottomAnchor),collectionView.trailingAnchor.constraint(equalTo: trailingAnchor),])
super.updateConstraints()
}
}
然后我写了一个 UIView 的扩展
func addShadow(){
layer.cornerRadius = 5.0
layer.borderColor = UIColor.clear.cgColor
layer.borderWidth = 5.0
layer.shadowOpacity = 0.5
layer.shadowColor = UIColor.lightGray.cgColor
layer.shadowRadius = 5.0
layer.shadowOffset = CGSize(width:5,height: 5)
layer.masksToBounds = true
}
我以 cell.contentView.addShadow() 的形式访问它
目前它看起来像这样,甚至在瓷砖之间没有任何垂直分隔符。 我想要的是每个瓷砖的单独卡片视图(吉姆和保罗) 我在这里错过了什么??任何帮助将不胜感激
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)