Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios setNeedsDisplay()未更新collectionViewCell子视图';s drawRect_Ios_Swift_Drawrect_Collectionview_Setneedsdisplay - Fatal编程技术网

Ios setNeedsDisplay()未更新collectionViewCell子视图';s drawRect

Ios setNeedsDisplay()未更新collectionViewCell子视图';s drawRect,ios,swift,drawrect,collectionview,setneedsdisplay,Ios,Swift,Drawrect,Collectionview,Setneedsdisplay,我试图在collectionView单元格内显示进度指示器。为此,有一个后台线程向主线程发送通知以更新进度指示器 在主视图控制器中 func updateProgressIndicator(notification:NSNotification){ let userInfo = notification.userInfo! as NSDictionary let date = userInfo["date"] as! NSDate let

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

在主视图控制器中

    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:)
时,我希望您得到的
DateCell的实例与集合视图当前显示的实例不同。正在显示的一个正在使用中,因此它不会从
dequeue…
方法返回。您可以通过在单元格上使用
NSLog
来测试我的判断是否正确。我希望这个地址与您在
cellForItemAtIndexPath()
中返回的地址不同


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

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


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

正如Rob所建议的那样,
dequeueReusableCellWithReuseIdentifier(\uUx:forIndexPath:)
导致了问题。创建单元格时将其存储在字典中,这样就可以直接引用它。这意味着将使用
setNeedsDisplay()

以下是更新的功能

var calendarDateCells:[NSDate:DateCell] = [:]

func updateProgressIndicator(notification:NSNotification){
    let userInfo = notification.userInfo! as NSDictionary
    let date = userInfo["date"] as! NSDate
    let myCell = self.calendarDateCells[date]
    if(myCell != nil){
        myCell!.showProgress()
    }
}


func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("DateCell", forIndexPath: indexPath) as! DateCell
    let date = calendarDates[indexPath.item]
    self.calendarDateCells[date] = cell
    return cell
}

正如Rob所建议的那样,
dequeueReusableCellWithReuseIdentifier(\uxForIndeXPath:)
导致了问题。创建单元格时将其存储在字典中,这样就可以直接引用它。这意味着将使用
setNeedsDisplay()

以下是更新的功能

var calendarDateCells:[NSDate:DateCell] = [:]

func updateProgressIndicator(notification:NSNotification){
    let userInfo = notification.userInfo! as NSDictionary
    let date = userInfo["date"] as! NSDate
    let myCell = self.calendarDateCells[date]
    if(myCell != nil){
        myCell!.showProgress()
    }
}


func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("DateCell", forIndexPath: indexPath) as! DateCell
    let date = calendarDates[indexPath.item]
    self.calendarDateCells[date] = cell
    return cell
}

谢谢就是这样。我创建了一个字典来保存单元格,因此每次在CollectionView中创建一个字典时,单元格都会被存储。然后,正如建议的那样,只需为所需的单元格调用
showProgress()
函数即可。@Rob Napier,我面临同样的问题,但即使在删除Beaqued时,我也拥有正确的单元格,因为我将其从可见单元格的列表中拉出,并与唯一标识符(单元格类中的一个变量)匹配,也匹配了地址,但仍然不起作用。你能看看这个吗:谢谢。。。就是这样。我创建了一个字典来保存单元格,因此每次在CollectionView中创建一个字典时,单元格都会被存储。然后,正如建议的那样,只需为所需的单元格调用
showProgress()
函数即可。@Rob Napier,我面临同样的问题,但即使在删除Beaqued时,我也拥有正确的单元格,因为我将其从可见单元格的列表中拉出,并与唯一标识符(单元格类中的一个变量)匹配,也匹配了地址,但仍然不起作用。你能看看这个吗