这是我的案例:
我有一个包含餐厅列表的表格,每个条目都在使用 drawRect 绘制的记分栏上显示随时间变化的检查结果。
当用户向下滚动表格然后向上滚动时,会显示带有黄色和红色方 block 的记分栏,之前的记分栏会选择这些方 block 。日期也被绘制到以前的记分栏中。
我的问题在于摆脱旧方 block 。它们不应该在每次调用 drawRect 时都被删除吗?
我在下面放了三张图片,希望能解释我的意思。
我不认为这是表行缓存自定义单元格的问题? 一旦 UITableCell 建立单元格出队,就会在此处绘制得分条;单元格中的所有其他 View 都是正确的,这让我认为问题在于 drawRect 未能清除图形上下文。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row < self.establishments.count) {
return [self establishmentCellForIndexPath:indexPath];
} else if (self._noResultsFound) {
return [self noResultsCell];
} else {
return [self loadingCell];
}
}
- (UITableViewCell *)establishmentCellForIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"EstablishmentCell";
DSFEstablishment *establishment = [self.establishments objectAtIndex:[indexPath row]];
DSFEstablishmentCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[cell setEstablishment:establishment];
[cell updateCellContent];
return cell;
}
在 updateCellContent 期间,我们尝试清除旧的 scorebarviews(如果存在),但代码从未找到 DSFScorebarView 类的其他 subview 。最后,scorebarView 被创建并添加到 View 的 subview 中。
- (void)updateCellContent {
NSLog(@"updateCellContent: %@", self.establishment.latestName);
self.name.text = self.establishment.latestName;
self.address.text = self.establishment.address;
self.type.text = self.establishment.latestType;
if (self.establishment.distance) {
self.distance.text = [NSString stringWithFormat:@"%.2f km", self.establishment.distance];
} else {
self.distance.text = @"";
}
// Clear out old scorebarView if exists
for (UIView *view in self.subviews) {
if ([view isKindOfClass:[DSFScorebarView class]]) {
NSLog(@">>>removeFromSuperview");
[view removeFromSuperview];
}
}
DSFScorebarView *scorebarView = [[DSFScorebarView alloc] initWithInspections:self.establishment.inspections];
[self addSubview:scorebarView];
得分栏 View 是在 initWithInspections 中使用 drawRect 绘制的,并确保得分栏的长度不超过 17 个方格(最近的检查)。每次检查都有一个绿色=低|黄色=中等|红色=高的方 block ,年份画在方 block 下方。
我已经设置了 self.clearsContextBeforeDrawing = YES,但它并没有解决问题。
// Custom init method
- (id)initWithInspections:(NSArray *)inspections {
CGRect scorebarFrame = CGRectMake(20, 38, 273, 22);
if ((self = [super initWithFrame:scorebarFrame])) {
self.inspections = inspections;
self.clearsContextBeforeDrawing = YES;
[self setBackgroundColor:[UIColor clearColor]];
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
float xOffset = 0; // Keep track of position of next box -- start at 1
float yOffset = 0; // Fixed Y offset. Adjust to match shadows
NSString *previousYear = nil;
UIFont *yearFont = [UIFont fontWithName:@"PFTempestaFiveCompressed" size:8.0];
// take subset/slice of inspections. only the last 17, so it will fit on screen.
int inspections_count = (int)[self.inspections count];
int startIndex = inspections_count > 17 ? inspections_count - 17 - 1 : 0;
int subarrayLength = inspections_count > 17 ? 17 : inspections_count;
NSArray *inspectionsSlice = [self.inspections subarrayWithRange:NSMakeRange(startIndex, subarrayLength)];
for (id inspection in inspectionsSlice) {
// Top of box
NSMutableArray *pos0RGBA = [inspection colorForStatusAtPositionRGBA:0];
CGFloat topColor[] = ...
CGContextSetFillColor(ctx, topColor);
CGRect box_top = CGRectMake(xOffset, yOffset, kScoreBoxWidth, kScoreBoxHeight / 2);
CGContextAddRect(ctx, box_top);
CGContextFillPath(ctx);
// Bottom of box
NSMutableArray *pos1RGBA = [inspection colorForStatusAtPositionRGBA:1];
CGFloat bottomColor[] = ...
CGContextSetFillColor(ctx, bottomColor);
CGRect box_bottom = CGRectMake(xOffset, kScoreBoxHeight / 2 + yOffset, kScoreBoxWidth, kScoreBoxHeight / 2);
CGContextAddRect(ctx, box_bottom);
CGContextFillPath(ctx);
// Year Text
NSString *theYear = [inspection dateYear];
if (![theYear isEqualToString:previousYear]) {
CGFloat *yearColor = ...
CGContextSetFillColor(ctx, yearColor);
// +/- 1/2 adjustments are positioning tweaks
CGRect yearRect = CGRectMake(xOffset+1, yOffset+kScoreBoxHeight-2, kScoreBoxWidth+50, kScoreBoxHeight);
[theYear drawInRect:yearRect withFont:yearFont];
previousYear = theYear;
}
xOffset += kScoreBoxWidth + kScoreBoxGap;
}
在下面实现@HaIR 的解决方案后,在点击表格行后,我在灰色背景上得到了一条白色 strip 。在添加额外宽度以“遮盖”年线之后,它非常难看。
- (void)drawRect:(CGRect)rect {
...
CGFloat backgroundColour[] = {1.0, 1.0, 1.0, 1.0};
CGContextSetFillColor(ctx, backgroundColour);
CGRect fullBox = CGRectMake(xOffset, yOffset, kScoreBoxWidth * 17, 2 * (kScoreBoxHeight-2));
CGContextAddRect(ctx, fullBox);
CGContextFillPath(ctx);
for (id inspection in inspectionsSlice) {
...
最佳答案
您已经接管了 DrawRect,因此它只会执行您在 dra 中手动执行的操作
在上面绘制任何内容之前,用背景色填充整个条形区域。
您正在重复使用单元格,因此,例如,您在 Grace Market/Maple Plaza 的第二个版本下方显示了 Turf Hotel Restaurant 图表。
CGContextSetFillColor(ctx, yourBackgroundColor);
CGRect fullBox = CGRectMake(xOffset, yOffset, kScoreBoxWidth * 17, kScoreBoxHeight);
CGContextAddRect(ctx, fullBox);
CGContextFillPath(ctx);
如果您只是清除单元格中的背景而不是使用背景颜色。然后:
CGContextClearRect(context, rect);
在您的 DrawRect 开头可能更合适。
修订:
仔细查看您的问题,根本问题是您没有找到并删除旧的 DSFScoreboardView。也许您应该尝试通过 subview 进行递归搜索。喜欢:
- (void)logViewHierarchy {
NSLog(@"%@", self);
for (UIView *subview in self.subviews) {
//look right here for your DSFScorebarView's
[subview logViewHierarchy];
}
}
@end
// In your implementation
[myView logViewHierarchy];
关于ios - 为什么 drawRect 会留下部分图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25478319/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
它不等于主线程的binding,这个toplevel作用域是什么?此作用域与主线程中的binding有何不同?>ruby-e'putsTOPLEVEL_BINDING===binding'false 最佳答案 事实是,TOPLEVEL_BINDING始终引用Binding的预定义全局实例,而Kernel#binding创建的新实例>Binding每次封装当前执行上下文。在顶层,它们都包含相同的绑定(bind),但它们不是同一个对象,您无法使用==或===测试它们的绑定(bind)相等性。putsTOPLEVEL_BINDINGput
我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?
我刚刚被困在这个问题上一段时间了。以这个基地为例:moduleTopclassTestendmoduleFooendend稍后,我可以通过这样做在Foo中定义扩展Test的类:moduleTopmoduleFooclassSomeTest但是,如果我尝试通过使用::指定模块来最小化缩进:moduleTop::FooclassFailure这失败了:NameError:uninitializedconstantTop::Foo::Test这是一个错误,还是仅仅是Ruby解析变量名的方式的逻辑结果? 最佳答案 Isthisabug,or