- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用自定义单元格制作一个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/
我一直在修改VS 2010模板。到目前为止,我已经可以从模板创建vb.net类库项目。 但是,一件小事困扰着我。 在我的项目模板中,默认程序集名称与默认文件名相同。我将根 namespace 留为空白
我有一个 NSTextField,里面有一个 NSNumberFormatter。我见过一些文本字段,如果你将它们留空,它只会将之前的数字放回其中。我很好奇 Interface Builder 中是否
sql问题 我有这样的表格: id | Animal | country | gender | qty 1 | cow | USA | male | 6 2 | cow |
我正在尝试制作一个包含内容页面的网站。 我使用 go to chapter 1作为内容部分,当它点击时,它会处理 Chapter 1 这很好用,但有 1 个问题(总有一个问题)...我想让它看起来像维
我正在制作一个包含 4 个主视图的应用。我希望所有这些都保留在顶部的 LIST 菜单中(现在没问题)。 使用 Fragments,我可以在不执行其他 Activity 的情况下更改 View ,但是由
我正在定义一个 Form 类,它有一个文件字段并且不需要向它添加文件......但是每当我不提供文件时,表单都是无效的......所以有什么方法可以让我可以让用户选择是否添加文件吗??? 最佳答案 是
所以我尝试了之前帖子中关于这个问题的一些方法,但似乎没有一个对我有用(毫无疑问是用户错误)。 基本上,如果编辑文本留空,我的计算按钮就会崩溃。我需要发生的是,如果留空,则 edittext 的值为 0
我正在尝试提取 BufferedImage 的 10 px 方形部分并将它们添加到新的 BufferedImage 中,与 this drawImage tutorial 中显示的困惑示例非常相似。但
所以我正在使用 Postgres 和 Hibernate 4.2.2 以及这样的实体 @Entity(name = "Users") @Check(constraints = "email ~* '^
我正在尝试将列名为“邮政编码”、“2010 年人口”、“Land-Sq-Mi”和“每平方英里密度”的 CSV 文件导入我的测试表,该表名为 derp--这就是我在开头使用 drop 语句的原因,这样我
我正在尝试使用 usemin 插件来缩小并创建我的可分发文件。这是我的 Gruntfile.js: module.exports = function(grunt) { require('matc
在我的 HTML 中我有一个 ,并在 内我需要不同的文本行之间的垂直间距。我通过使用 实现了这一点具有对应于我想要的间距量的 CSS 类的元素。 例如,对于 5px 的间隙,我使用 与 heig
我有一个函数触发两个观察者 - keyboardWillShowNotification 和 KeyboardWillHideNotification,我基本上做的是在编辑时重置文本字段,模拟 的效果
这是一个非常奇怪的问题: Screenshoot 我确保图像大小相等,并且 Chrome 可以正确显示。为什么不是 IE 和 Firefox?对此有什么解决方案吗? 最佳答案 一个 li 的高度与另一
我是一名优秀的程序员,十分优秀!