在. ThumbView
_ UICollectionViewCell
以下代码在 iOS 7 模拟器中运行良好:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
ThumbView *thumbView = (ThumbView *)[cell viewWithTag:1];
[thumbView setNeedsDisplay];
return cell;
}
但是,在 iOS 6 版本的模拟器中:
网格都消失了:
我在 ThumbView 中放了一个NSLog
以drawRect
确保drawRect
被调用。
怎么了?
- Xcode 版本 5.0 (5A1412)
- iOS 模拟器版本 7.0 (463.9.4)
更新(绘图代码):
- (void)drawRect:(CGRect)rect
{
NSLog(@"%s", __func__);
[self drawGrid:rect];
}
- (void)drawGrid:(CGRect)rect
{
CGFloat margin = self.margin;
CGFloat lineWidth = 2.0;
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect gridRect = CGRectInset(rect, margin, margin);
for (NSInteger i = 1; i < 9; i++) {
CGFloat h = gridRect.size.height / 9;
CGFloat y = i * h;
CGContextMoveToPoint(context, margin, y+margin);
CGContextAddLineToPoint(context, gridRect.size.width+margin, y+margin);
CGContextSetLineWidth(context, lineWidth/4.0);
if (i == 3 || i == 6) {
CGContextSetStrokeColorWithColor(context, UIColor.darkGrayColor.CGColor);
} else {
CGContextSetStrokeColorWithColor(context, UIColor.lightGrayColor.CGColor);
}
CGContextStrokePath(context);
}
for (NSInteger i = 1; i < 9; i++) {
CGFloat w = gridRect.size.width / 9;
CGFloat x = i * w;
CGContextMoveToPoint(context, x+margin, margin);
CGContextAddLineToPoint(context, x+margin, gridRect.size.height+margin);
CGContextSetLineWidth(context, lineWidth/4.0);
if (i == 3 || i == 6) {
CGContextSetStrokeColorWithColor(context, UIColor.darkGrayColor.CGColor);
} else {
CGContextSetStrokeColorWithColor(context, UIColor.lightGrayColor.CGColor);
}
CGContextStrokePath(context);
}
CGFloat frameWidth = lineWidth * 2.0;
CGRect frameRect = CGRectInset(rect, 0.0, 0.0);
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) frameWidth = lineWidth * 2.0;
CGContextSetLineWidth(context, frameWidth);
CGContextSetStrokeColorWithColor(context, UIColor.darkGrayColor.CGColor);
CGContextStrokeRect(context, frameRect);
}