How to draw graphic objects within drawRect at dif

2020-05-05 00:49发布

For a particular controller class, I need to draw a grid (the grid refers to a daily schedule, which changes each day, sometimes each hour.). Some time later, upon an action by the user, I need to fill that grid with updated schedule data that I did not have when I was doing the original grid.

My question is: since I don't know the selected day at the time the viewDidLoad, does this mean I have to re-draw the entire grid again, along with the new daily schedule data? (that's the only way I can think of to get everything drawn in the drawRect) Or can I call a method outside of drawRect to draw the daily schedule? or is there another, better way of doing this?

标签: objective-c
2条回答
混吃等死
2楼-- · 2020-05-05 01:28

It sounds to me like you'll want to separate out the drawing code for the grid from the drawing code for the schedule data.

The easiest way to do this is to create a subclass of NSView for your schedule data- then you can create an instance of that object for each piece of data. Your grid view can position those schedule data objects by setting their frame and the schedule data objects can draw themselves in their own drawRect: method.

查看更多
Viruses.
3楼-- · 2020-05-05 01:43

You don't specify whether this is iOS or Mac, which might very marginally affect what assumptions you are able to make about this. But in general a view's drawRect method should at least redraw everything within the supplied rectangle (the clue is in the method name). And, broadly speaking, that is the place to manage such drawing, though of course you can break the code up into other methods for clarity and organisation.

Unless your grid is incredibly dense, it is unlikely that this redraw will have noticeable impact on performance. Failing to redraw needed content in actually dirty regions would be a much worse situation.

查看更多
登录 后发表回答