スケジュールのように機能するUIViewがあります(XCode 4.6、iOS 6.2、およびストーリーボードを使用して作成されたiPadアプリ)。私は実際に2つのUIVIewを持っています。1つはウィンドウの上半分に、もう1つは下半分にあります。上半分にカレンダーがあります。そしてその下の時間のグリッド。ユーザーが予定のある日をタップすると、下部のUIViewに描画されます。ユーザーは予定のある任意の日をタップでき、古いものは削除され、新しいデータは底面図に描画されます。
タップされたその日の予定がない場合を除いて、これはうまく機能します。その場合、古いデータは何らかの方法で削除されるのではなく、残ります。[self setNeedsDisplay]を試しましたが、何も変わっていないので何も描かれていません。UIViewのコンテンツを無効にして、データなしで強制的に再描画する方法はありますか?
更新:これが予定を描画するコードです:
- (void)drawRect:(CGRect)rect {
// load dictionary files from plists
NSString *path = [[NSBundle mainBundle] bundlePath];
// NSLog(@"\n\npath %@",path);
//
NSString *timesFinalPath = [path stringByAppendingPathComponent:@"startingTimes.plist"]; // do starting times
NSDictionary *startDict = [NSDictionary dictionaryWithContentsOfFile:timesFinalPath];
NSString *namesFinalPath = [path stringByAppendingPathComponent:@"techNames.plist"]; // do tech names
NSDictionary *techDict = [NSDictionary dictionaryWithContentsOfFile:namesFinalPath];
// NSLog(@"\nPath: %@\ntechDict.count: %d\nstartDict.count %d", path, techDict.count, startDict.count);
// NSLog(@"nbr of recognizers: %lu", (unsigned long)self.gestureRecognizers.count);
// find out how many appointments we have for this day
SingletonAppointments *sharedInstance = [SingletonAppointments sharedInstance]; // initialize
if(sharedInstance.globalApptList.count > 0) {
for(int i = 0; i < sharedInstance.globalApptList.count; i++) { // get customer apointments for this day
AppointmentInfo *apptObject = [sharedInstance.globalApptList objectAtIndex:i];
//NSLog(@"apptObject.aApptKey: %@", apptObject.aApptKey);
// using apptObject.aApptKey, get the client's name
NSPredicate *predicate = ([NSPredicate predicateWithFormat:@"(aClientKey = %@)", apptObject.aApptKey]);
clientInfo = [ClientInfo MR_findAllWithPredicate: predicate];
NSString *cliInfo;
for (ClientInfo *cli in clientInfo) {
cliInfo = cli.aClientName; // found client name
}
// settings for name drawing
const float nameFontSize = 14;
UIFont *hourfont=[UIFont systemFontOfSize: nameFontSize];
// now compute the duration
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"HH:mm"];
NSTimeInterval interval = [apptObject.aEndTime timeIntervalSinceDate:apptObject.aStartTime];
int hours = (int)interval / 3600; // integer division to get the hours part
int minutes = (interval - (hours*3600)) / 60; // interval minus hours part (in seconds) divided by 60 yields minutes
// NSString *duration = [NSString stringWithFormat:@"%d:%02d", hours, minutes];
// localize time
NSDate *sTime = [self localizeDate: apptObject.aStartTime];
// NSDate *eTime = [self localizeDate: apptObject.aEndTime];
// NSLog(@"\n\nsTime: %@ eTime: %@", sTime, eTime);
// NSLog(@"\nlocalized sTime: %@\nlocalized eTime %@", sTime, eTime);
// determine time slot (dictiionary starts at 0900... must accomodate for that)
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]]; //Optionally for time zone converstions
[formatter setDateFormat:@"HH:mm"];
// get time shop opens and closes
PreferenceData *prefDataFound = [PreferenceData MR_findFirst];
if(prefDataFound) { // fill the times
startHour = prefDataFound.aShopOpens;
endHour = prefDataFound.aShopCloses;
}
// NSLog(@"startHour: %@ endHour: %@",startHour, endHour);
// now, get first time slot in dictionary
NSArray *ts = [startDict allKeysForObject:[NSNumber numberWithInt:5]]; // comes back with one entry: 9:00
// clean it (remove colon)
NSString *dictValue = ts[0];
NSString *cleanDictValue = [dictValue stringByReplacingOccurrencesOfString:@":" withString:@""];
// convert to int
int dictStartTime = [cleanDictValue intValue];
int intStartHour = [startHour intValue];
// compute gridTimeOffset
float gridTimeOffset = dictStartTime - intStartHour;
NSString *stringFromDate = [formatter stringFromDate:sTime];
// NSLog(@"\n\nstringFromDate: %@",stringFromDate);
// NSLog(@"\nserviceTech: %@", apptObject.aServiceTech);
// find the correct column by tech name
NSNumber *techColumn = [techDict objectForKey:apptObject.aServiceTech];
// NSLog(@"\nserviceTech: %@, techColumn: %@", apptObject.aServiceTech, techColumn);
// set the context
CGContextRef context = UIGraphicsGetCurrentContext();
// draw the name... (x refers to column; y refers to time slot)
[[UIColor blueColor] set]; // sets color of customer name
NSNumber *timeSlot = [startDict objectForKey:stringFromDate];
float correctedSlot = [timeSlot floatValue] + gridTimeOffset; // corrected slot info for name and rectangle
[cliInfo drawAtPoint:CGPointMake([techColumn floatValue], correctedSlot) withFont:hourfont];
// NSLog(@"\ntimeSlot: %@ adjusted: %@", [timeSlot floatValue], ([timeSlot floatValue] - gridTimeOffset));
// make some settings for the blocking
UIGraphicsBeginImageContext(self.bounds.size);
// CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
// CGContextSetLineWidth(context, 1.0);
// get the CGContextRef for drawing the rectangle
CGContextSetFillColorWithColor(context, [[[UIColor grayColor] colorWithAlphaComponent: 0.3] CGColor]);
// NSNumber *startPos = [startDict objectForKey:stringFromDate];
int shadeHeight = ((hours * 4) + (minutes / 15)) * 25; // compute starting y value
// CGContextFillRect(context, CGRectMake([techColumn floatValue], [startPos floatValue], 218, (float)shadeHeight));
CGContextFillRect(context, CGRectMake([techColumn floatValue], correctedSlot, 218, (float)shadeHeight));
UIGraphicsEndImageContext(); // end the context so the other appointments will draw
}
}
}