我正在使用自定义单元格制作一个UICollectionView
,并且发生了一件非常奇怪的事情。所有 indexPath.row
为 ODD 数字的单元格都留空,我无法对它们执行任何绘图。
我使用 Storyboard 在自己的 UIViewController
中创建了一个 UICollectionView
对象。 UICollectionView
的单元格设置为我的自定义 UICollectionViewCell
子类,名为 CustomCell。每个单元格占据 UICollectionView
的整个宽度和高度。 CustomCell 内的所有内容都是以编程方式创建的,而不是使用 Storyboard。这是我的 cellForItemAtIndexPath
代码:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = (CustomCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
//createViewWithDay is used to populate the contents of the cell
[cell createViewWithDay:indexPath.row isToday:YES withSize:cell.frame.size];
NSInteger x = indexPath.row;
NSLog(@"Path: %i", x);
return cell;
}
每个CustomCell都会创建一个自定义 View (名为CustomView),并将其添加为 subview 。现在,CustomView 所做的只是绘制 X 轴和 Y 轴。
奇怪的是,每个单元格都会正确触发cellForItemAtIndexPath
。也就是说,偶数和奇数索引都需要它。与委托(delegate)方法 didSelectItemAtIndexPath
相同。每个CustomView的绘图不会根据单元格的索引而改变。事实上,该指数没有任何变化。以下是我运行应用程序时出现的示例。
。
在第一张图片中,绘制轴的单元格位于 indexPath.row == 14
处,而黑色单元格位于 indexPath.row == 15
处。在第二张图片中,索引 15 位于左侧,索引 16 位于右侧。
有人知道为什么会发生这种情况吗?奇数/偶数索引可能无关紧要,但这就是正在发生的事情。
编辑:
一些额外的代码..
这是 createViewWithDay
,它是在 cellForItemAtIndex
方法中调用的:
- (void)createViewWithDay:(float)day isToday:(BOOL)today withSize:(CGSize)size
{
CustomView *newView = [[CustomView alloc] initWithFrame:self.frame];
[newView viewForDay:day overDays:6 withDetail:20 today:YES withSize:size];
[newView setBackgroundColor:[UIColor whiteColor]];
[self addSubview:newView];
}
这是viewForDay
- (void)viewForDay:(NSInteger)primaryDay overDays:(NSInteger)days withDetail:(NSInteger)detail today:(BOOL)today withSize:(CGSize)size
{
_graphPrimaryDay = primaryDay;
_numberOfDays = days;
_lineDetail = detail;
_isToday = today;
_viewSize = self.frame.size;
_pointsPerDay = (float)(_lineDetail / _numberOfDays);
_isReadyToDraw = YES;
[self createGraphDays];
}
此 viewForDay
方法只是分配一些 CustomView 实例变量,而 createGraphDays
方法则使用虚拟数据填充 NSMutableArray。
我想我还应该添加 CustomView 的 drawRect
方法,所以这里是..
- (void)drawRect:(CGRect)rect
{
if(_isReadyToDraw)
{
[self drawGraph];
}
}
这是drawGraph
..
- (void)drawGraph
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(context, 0, _viewSize.height / 2);
CGContextAddLineToPoint(context, _viewSize.width, _viewSize.height / 2);
if(_isToday)
{
CGContextMoveToPoint(context, _viewSize.width / 2, 0);
CGContextAddLineToPoint(context, _viewSize.width / 2, _viewSize.height);
}
CGContextSetLineWidth(context, 2.5);
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
CGContextStrokePath(context);
}
谢谢!
最佳答案
我相信我已经找到了问题的解决方案。在前两个单元格(索引 0 和 1)之后,我的自定义 View 的 drawRect
方法不再被调用。无论出于何种原因,自定义 View 都被自定义单元格的背景颜色(黑色)覆盖。因此,我通过在自定义 View 上调用 setNeedsDisplay
来修复此问题,以便强制为每个单元格绘制正确的图像。
编辑:
对于这个问题实例来说,这是正确的答案,但我实际上由于不同的原因遇到了同样的问题。
对于旧的解决方案,我所做的只是确保 [cell createViewWithDay:isToday:withSize:]
(从 cellForRowAtIndexPath
调用)包含 [self.view setNeedsDisplay]
,以便手动更新单元格的 View 。
但是,正如我所说,我遇到了同样的问题,其他所有单元格都留空,但原因不同。在 cellForRowAtIndexPath 中,我调用了单元格的设置方法。该方法依次调用 subview 的设置方法。 subview 设置方法的参数之一是单元格的框架,因此它将类似于(在collectionViewCellSubclass
中)[self.view setupViewWithFrame:(CGRect)frame]
.
对于第一个单元格,效果很好,但对于第二个单元格,传递到 View 的帧是(320, 0, 320, 250)
。由于单元格相对于 Collection View 的原点为 (320, 0)
,因此单元格 subview 的原点始终为 (320, 0)
,但相对于单元格,所以它实际上会被放置在屏幕外。通过仅初始化 subview 的大小并确保其原点为 (0, 0)
即可修复此问题。整个问题现在已经变得非常长了,但希望这个替代问题和解决方案会有所帮助。
关于iphone - UICollectionView奇数单元格留空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19260349/