Ios 为什么不';更新流布局时,UICollectionView中的单元格是否会更新其大小?
我在UICollectionViewController中有一个单元格网格: 该应用程序的总体思路是,用户点击一个单元格,该单元格将对指定的时间进行倒计时,一旦计时器完成,它将在下次解锁。第一次始终处于解锁状态: 连续两次点击同一单元格将删除除第一个单元格和被点击两次的单元格以外的所有单元格,以允许用户循环。Ios 为什么不';更新流布局时,UICollectionView中的单元格是否会更新其大小?,ios,xcode,swift,uicollectionview,Ios,Xcode,Swift,Uicollectionview,我在UICollectionViewController中有一个单元格网格: 该应用程序的总体思路是,用户点击一个单元格,该单元格将对指定的时间进行倒计时,一旦计时器完成,它将在下次解锁。第一次始终处于解锁状态: 连续两次点击同一单元格将删除除第一个单元格和被点击两次的单元格以外的所有单元格,以允许用户循环。 执行此操作还应将collectionViewLayout更新为只有两列宽度相等的列,填充屏幕,但此时第一个和第二个单元格未调整大小: [连续点击两次按钮后](http://i.imgu
执行此操作还应将collectionViewLayout更新为只有两列宽度相等的列,填充屏幕,但此时第一个和第二个单元格未调整大小: [连续点击两次按钮后](http://i.imgur.com/cLvaApG.png) 布局肯定会更新/无效,因为旋转设备会按预期更新单元格: [旋转到横向模式](http://i.imgur.com/rngwzre.png) [并旋转回纵向](http://i.imgur.com/ajwPr1J.png) 我正在按如下方式调整单元格的大小:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
if repeating {
let target = collectionView.frame.width
let targetFraction = target / 2
return CGSize(width: targetFraction, height: targetFraction)
}
let target = CGFloat(125)
let frameRef = collectionView.frame.width
let frameWidthRemainder = frameRef % target
let frameWidthRounded = frameRef - frameWidthRemainder
let columnCount = frameWidthRounded / target
let columnWidth = frameRef / columnCount
return CGSize(width: columnWidth, height: columnWidth)
}
Repeating
是一种bool,作为操作的一部分,通过点击单元格进行设置/重置:
@IBAction func circleProgressButtonTapped(sender: CircleProgressButton) {
...
if previouslySelectedButton == sender && !repeating {
repeating = true
collectionViewLayout.invalidateLayout()
buttons = [buttons[0], buttons[row]]
collectionView?.reloadData()
for button in buttons {
button.circleProgressButton.setNeedsDisplay()
}
}
...
}
当设备旋转时,我将使用以下命令重置布局:
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
for button in buttons {
button.circleProgressButton.setNeedsDisplay()
}
collectionViewLayout.invalidateLayout()
}
按钮
是一个单元格数组,由以下内容填充/重置:
func refreshView() {
if let min = defaults.valueForKey(userDefaultsKeys.min.rawValue) as? Int {
self.min = min
}
if let max = defaults.valueForKey(userDefaultsKeys.max.rawValue) as? Int {
self.max = max
}
buttons = []
collectionView?.reloadData()
for i in 0..<buttonCount {
guard let collectionView = collectionView else { break }
let indexPath = NSIndexPath(forRow: i, inSection: 0)
let multiplier = i + 1
let time = min * multiplier
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CircleProgressButtonCollectionViewCell
cell.circleProgressButton.delegate = self
cell.circleProgressButton.value = time
cell.circleProgressButton.resetText()
cell.circleProgressButton.indexPath = indexPath
cell.circleProgressButton.progress = 0
cell.circleProgressButton.activeProgressColour = theme
cell.circleProgressButton.tintColor = theme
cell.circleProgressButton.setNeedsDisplay()
if i > 0 {
cell.circleProgressButton.enabled = false
}
buttons.append(cell)
}
}
我尝试过将按钮设置为空,重新加载,然后填充(生成全新的单元格)并重新加载,但即使这样也不起作用
如果我必须彻底改变我的整个方法,那就这样吧。无法打开你提到的链接进行检查images@Anny由于缺乏声誉,我不能发布图片或超过2个URL。前两个链接可以正常工作,因为其他链接占用了http://和其余链接之间的空间。无法打开您提到的链接进行检查images@Anny由于缺乏声誉,我不能发布图片或超过2个URL。前两个链接工作正常,因为其他链接占用了http://和其余链接之间的空间。
if previouslySelectedButton == sender && !repeating {
...
collectionViewLayout.prepareLayout()
if let collectionView = collectionView {
collectionView.setNeedsDisplay()
collectionView.setNeedsLayout()
}
if let collectionViewLayoutCollectionView = collectionViewLayout.collectionView {
collectionViewLayoutCollectionView.setNeedsDisplay()
collectionViewLayoutCollectionView.setNeedsLayout()
}
collectionView?.setNeedsDisplay()
collectionViewLayout.collectionView?.setNeedsDisplay()
collectionView?.setNeedsLayout()
collectionViewLayout.collectionView?.setNeedsLayout()
collectionView?.reloadData()
buttons = [buttons[0], buttons[row]]
for button in buttons {
button.setNeedsLayout()
button.setNeedsDisplay()
}