gpt4 book ai didi

ios - setNeedsDisplay() 没有更新 collectionViewCell subview 的 drawRect

转载 作者:行者123 更新时间:2023-11-28 08:45:04 25 4
gpt4 key购买 nike

我正在尝试在 collectionView 单元格内显示进度指示器。为此,有一个后台线程向主线程发送通知以更新进度指示器。

在主视图 Controller 中...

    func updateProgressIndicator(notification:NSNotification){
let userInfo = notification.userInfo! as NSDictionary
let date = userInfo["date"] as! NSDate
let percentComplete = userInfo["percent"] as! Double

self.progressIndicator.text = "\(percentComplete)%" // this works
let dateIndex = self.calendarDates.indexOf(date)
let indexPath = NSIndexPath(forItem: dateIndex!, inSection: 0)
let cell = self.collectionView.dequeueReusableCellWithReuseIdentifier("DateCell", forIndexPath: indexPath) as! DateCell
cell.showProgress()
}

该函数定位要更新的单元格的 indexPath。然后调用单元格的 showProgress 方法。

class DateCell: UICollectionViewCell {
@IBOutlet weak var dotGraph: DotGraphView!

func showProgress(){
print(" DateCell showProgress") // this does get printed
self.setNeedsDisplay()
}

override func drawRect(rect: CGRect) {
print(" DateCell drawRect") // this never gets printed
//drawing functions go here
}
}

为正确的单元格调用 showProgress() 方法并显示打印消息。但是,当 showProgress 方法调用 setNeedsDisplay() 时,drawRect 函数永远不会执行。

我更新单元格的唯一方法是使用 reloadRowsAtIndexPaths 完全重新加载单元格,但这应该是不必要的。

关于如何调用 drawRect 函数有什么想法吗?

最佳答案

您说 showProgress() 是为正确的单元格调用的,但这似乎不太可能。当您调用 dequeueReusableCellWithReuseIdentifier(_:forIndexPath:) 时,我希望您获得与 Collection View 当前显示的实例不同的 DateCell 实例。显示的正在使用中,因此它不会从 dequeue... 方法返回。您可以通过在单元格上使用 NSLog 来测试我对此是否正确。我希望这个地址与您在 cellForItemAtIndexPath() 中返回的地址不同。

与其将单元格出队,不如将单元格放入一个属性中,以便每个人都使用相同的单元格。这也是您应该在 cellForItemAtIndexPath() 中返回的单元格。

关于ios - setNeedsDisplay() 没有更新 collectionViewCell subview 的 drawRect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35538106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com