我得到以下代码,我从我的sql表中选择一个随机值,但我想阻止它选择重复。
private var cachedDecorationView = [UICollectionViewLayoutAttributes]()
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
//Tip: layoutAtributes with the same indexPath may can not be allowed for reusing
guard let atributes = super.layoutAttributesForElements(in: rect) else {
return nil
}
var mutatingAtributes = atributes
//find max Y position of the cells
var position = CGRect.zero
position.size.width = rect.size.width
position.size.height = shelfSize
for atribute in mutatingAtributes {
atribute.zIndex = 1
if atribute.frame.maxY > position.origin.y {
position.origin.y = atribute.frame.maxY
if rect.intersects(position) {
let shelf = prerareShelves(for: position)
mutatingAtributes.append(shelf)
}
}
}
return mutatingAtributes
}
func prerareShelves(for rect: CGRect) -> UICollectionViewLayoutAttributes {
for cache in cachedDecorationView {
if cache.frame == rect {
return cache
}
}
let indexForNewDecoration = Int(rect.origin.y/itemSize.height + shelfSize)
let decoratorView = layoutAttributesForDecorationView(ofKind: DecorationViewKind.shelfView.rawValue, at: IndexPath(item: indexForNewDecoration, section: 0))!
decoratorView.frame = rect
cachedDecorationView.append(decoratorView)
return decoratorView
}
override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
return collectionView?.bounds.size != newBounds.size
}
override func invalidateLayout() {
super.invalidateLayout()
//delete all cached decoration views before new layout process
cachedDecorationView.removeAll()
}
在这种情况下,目标就像是火种。你得到一张随机图片(在一个随机的人身上)并投票,但那时已经投票的图片不应该从我的表中重新选择。我的目标是将所选值与用户表中的值进行比较,其中每个id都标记为已经由他投票。 有没有人知道如何实现这样的目标?
最诚挚的问候!
答案 0 :(得分:0)
您需要为用户perviews创建一个表,该表存储之前为此用户检索的数字
用户表
+------+--------+--------+
|USERID|USERDATA|USERDATA|
+------+--------+--------+
|1 | XXXX | XXXX |
+------+--------+--------+
|2 | AAAA | AAAA |
+------+--------+--------+
|3 | ZZZZ | ZZZZ |
+------+--------+--------+
用户视图表
+------+---------+
|USERID|pictureID|
+------+---------+
|1 | 1 |
+------+---------+
|1 | 2 |
+------+---------+
|2 | 3 |
+------+---------+
|3 | 3 |
+------+---------+
|3 | 2 |
+------+---------+
图片表
+----------+----+
|PICTUIREID|DATA|
+----------+----+
|1 |WWWW|
+----------+----+
|2 |EEEE|
+----------+----+
|3 |RRRR|
+----------+----+
然后你的查询将是
SELECT p.pictureID FROM Userviews uv
join picture p on p.PICTUIREID = uv.PICTUIREID
where uv.USERID <> (userID )
ORDER BY RAND() LIMIT 1"