Search code examples
iosuiviewios7cgcontext

How can I clear drawing of old data using CGContext methods?


I have an iPad app where I draw a grid of dates along the x axis and times down the y axis. I then draw colored bars (using CGContext methods) in specific sections of this grid (see image). enter image description here

When I try to re-draw the grid, the old bars are still there! How do I clear the old bars out of there? I've tried everything I could find on Google and SO, but nothing seems to work.

UPDATE 1: here is the "driving code"... note that there is no use of CGRect

CGContextRef currentContext = UIGraphicsGetCurrentContext();  // Get the current graphics context
// Start the line at this point (x,y)
CGContextMoveToPoint(currentContext, column, startPosY);

// compute end point  (additional fDurationSegments takes line width into consideration)
CGContextAddLineToPoint(currentContext, column,  startPosY + (fDurationSegments * FIFTEEN_MINUTE_INCREMENT));

//  draw the colored appointment line
CGContextSetLineDash(currentContext, 0, nil, 0);  //  reset dashed line to straight line
CGContextSetLineWidth(currentContext, LINE_WIDTH);  // Set the width for the lines

CGContextStrokePath(currentContext);  //  draw 'em

UPDATE 2: I put another UIView on top of the grid's view, made it transparent and drew the bars... still didn't replace the old stuff with the new.


Solution

  • From the Apple Developer's Forum:

    In order to get this all to work, the viewController needs to be involved. The viewController needs an IBOutlet to the actual WeeksAppts view that was created in the storyboard. You can create the IBOutlet by control-dragging from the view in storyboard to the viewController source code. The IBOutlet in the view controller will look something like this

    @property (weak, nonatomic) IBOutlet WeeksAppts *weeksApptsView;
    

    When something changes, the view controller needs to force an update to the view with a line of code like this

    [self.weeksApptsView setNeedsDisplay];