我有一个UIView,就像一个日程表(使用XCode 4.6,iOS 6.2和Storyboard编写的iPad应用程序)。我实际上有两(2)个UIVIews,一个在窗口的上半部分,另一个在下半部分。上半部分有一个日历;和一个低于它的网格。当用户点击具有约会的那一天时,它们将被绘制在底部的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
}
}
}
答案 0 :(得分:1)
这应该在控制底部视图的视图控制器的回调中处理。 (如果您有一个控制顶部和底部的视图控制器,则同样适用。)在其更新方法(应在选择一天时调用,包括空天)时,应该有类似的内容。
-(void)update {
if (selectedDay.events && selectedDay.events.count) {
// do your normal update
}
else {
// empty the view
}
}
请注意,如果底部视图是或具有表视图,则重置数据数组或集(上面引用为selectedDay.events
)并调用
[_tableView reloadData];
如果您有其他方案,则必须“手动”清空它。
修改强>
正如spokane-dud指出的那样,数据源单身人士存在缺陷。在适当的时候,数据必须明确地设置为空。
if(appointmentInfo.count > 0) {
for (AppointmentInfo *appt in appointmentInfo) {
sharedInstance.globalApptList = appointmentInfo; // move data to sharedInstance
}
}
else {
sharedInstance.globalApptList = nil;
}