UICollectionView未正确更新 - 使用THStringyFlowLayout(UICollectionView is not updating properly - used THStringyFlowLayout)
当我的应用程序启动时,它会从持久性中获取数据并在我的集合视图中显示5个单元格。 这些单元格看起来像普通的TableView单元格,但我使用Collection View进行后续的布局调整。 整个屏幕需要8个单元格。
之后的第二秒,使用19个单元格的数据更新模型,并通过调用以下内容更新集合视图:
[self.collectionView reloadData];
已经显示5个单元格的那些用新数据很好地刷新,但就是这样。 直到我触摸屏幕并向下滚动单元格的高度,才会显示其他单元格。 然后立即显示3个单元格并填充屏幕。 从那一刻起,一切都很好,但在滚动之前,它没有正确更新。
当我调试它时,我看到,在调用reloadData之后调用collectionView:numberOfItemsInSection:它返回正确的数字19,但是只询问那些5个单元格的collectionView:cellForItemAtIndexPath:(直到我滚动)。
有点绝望,谢谢你的帮助。
编辑 - 添加源代码:
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return [self.manager.jubilees count]; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { DLog(@"asked for cell: %i", indexPath.row); //just for debugging to know, what is going on JUJubileeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"JubileeCell" forIndexPath:indexPath]; cell.jubilee = self.manager.jubilees[(NSUInteger) indexPath.row]; return cell; } // called when model is updated -(void)jubileesUpdated { DLog(@"jubileesUpdated"); [self.collectionView reloadData]; }
编辑2:当加载前5个单元格时,[self.collectionView contentSize]为(width = 320,height = 358)。 当加载所有19个单元格并调用reloadData时,contentSize为(width = 320,height = 1366) - 但实际上并未显示单元格:(。
编辑3:问题是由使用THStringyFlowLayout引起的 - 如果我找到原因,我会发布更多内容。
When my app starts, it takes data from the persistence and displays 5 cells in my Collection View. These cells looks like normal TableView cells, but I use Collection View for later layout tweaks. It takes 8 cells to fill whole screen.
A second after that, the model is updated with the data for 19 cells and the Collection View is updated by calling:
[self.collectionView reloadData];
Those already displayed 5 cells are nicely refreshed with new data, but that's it. The other cells are not displayed until I touch the screen and scroll down about the height of the cell. Then instantly 3 cells are displayed and fills the screen. From that moment, everything is fine, but until I scroll, it is not updated correctly.
When I debugged it, I see, that the collectionView:numberOfItemsInSection: is called after calling reloadData and it returns correct number 19, but the collectionView:cellForItemAtIndexPath: is asked just for those 5 cells (until I scroll).
Kind of desperate here, thanks for your help.
Edit - added source code:
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return [self.manager.jubilees count]; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { DLog(@"asked for cell: %i", indexPath.row); //just for debugging to know, what is going on JUJubileeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"JubileeCell" forIndexPath:indexPath]; cell.jubilee = self.manager.jubilees[(NSUInteger) indexPath.row]; return cell; } // called when model is updated -(void)jubileesUpdated { DLog(@"jubileesUpdated"); [self.collectionView reloadData]; }
edit 2: When first 5 cells are loaded, the [self.collectionView contentSize] is (width=320, height=358). When the all 19 cells are loaded and reloadData called, contentSize is (width=320, height=1366) - but cells are not actually displayed :(.
edit 3: The problem is caused by using THStringyFlowLayout - I will post more, if I find the reason.
原文:https://stackoverflow.com/questions/22335503
最满意答案
借助jquery,您可以获得渲染图像的高度。
然后,您只需更改偏移参数。 这是一支笔: https : //codepen.io/anon/pen/KyQddz
<div class="navbar"> This is my navbar </div> <figure> <img src="https://via.placeholder.com/350x80" /> </figure> * { margin: 0; padding:0; box-sizing: border-box; } .navbar { background: red; width: 100%; height: 60px; position: fixed; top:0; } .shrink { background: green; } figure { height: 2000px; } img { display: block; width: 100%; height: auto; } var imgHeight; $(window).on('load', function () { imgHeight = $('img').height(); }); $(window).scroll(function() { if ($(document).scrollTop() > imgHeight) { $('.navbar').addClass('shrink'); } else { $('.navbar').removeClass('shrink'); } });
如果你想让导航栏只在你滚动图像的高度后才能修复,只需要移动
position:fixed;top:0;
到.shrink
类。.navbar { background: red; width: 100%; height: 60px; } .shrink { background: green; position: fixed; top:0; }
You can get the height of your rendered image thanks to jquery.
Then, you simply change the offset parameter. Here is a pen: https://codepen.io/anon/pen/KyQddz
<div class="navbar"> This is my navbar </div> <figure> <img src="https://via.placeholder.com/350x80" /> </figure> * { margin: 0; padding:0; box-sizing: border-box; } .navbar { background: red; width: 100%; height: 60px; position: fixed; top:0; } .shrink { background: green; } figure { height: 2000px; } img { display: block; width: 100%; height: auto; } var imgHeight; $(window).on('load', function () { imgHeight = $('img').height(); }); $(window).scroll(function() { if ($(document).scrollTop() > imgHeight) { $('.navbar').addClass('shrink'); } else { $('.navbar').removeClass('shrink'); } });
If you want the navbar the be fixed only after you scroll the height of the image, just move
position:fixed;top:0;
to the.shrink
class..navbar { background: red; width: 100%; height: 60px; } .shrink { background: green; position: fixed; top:0; }
相关问答
更多-
只需在您的身体上添加一个与您的导航栏高度相当的margin-top ,如下所示: body { margin-top: 50px; } 注意用导航栏的高度替换50px 。 Just add a margin-top to your body equivalent to your navbar height like this: body { margin-top: 50px; } N.B. Replace 50px with height of your navbar.
-
让文本显示在导航栏下(Getting text to appear under navbar)[2022-08-10]
如果科林说什么不行 - 应该 - 只是尝试打字 text-align: center If what Colin said doesn't work - which should - just try typing text-align: center -
我会建议你清除浏览器的缓存,有时是因为缓存超载,我们没有得到图像。 Firstly, there are several things with the controller. The latest post route doesn't render cover-properties, it is like below: return request.render("website_blog.latest_blogs", { 'posts': posts, 'pager': ...
-
为什么使用.header位置固定? 它由nav header重叠{width:100%; 身高:100px; top: 0; } use try this why using .header position fixed ?? it overlap by nav header { width: 100%; height: 100px; top: 0; } use try this
-
斜杆上的Navbar(Navbar on Slanted Div)[2023-04-06]
当使用float时,你需要清除,所以下面的元素看起来正确。 我在这里添加: https : //jsfiddle.net/44x11g34/一个例子。 我在两个主要块之间添加了什么: 和一些小css .clear { clear: both; } 希望这会帮助你。 When using float, you need to clear, so the folllowing elements to look right. I have added h ... -
您应该将类.navbar-brand添加到 检查jsFiddle: https ://plnkr.co/edit/uquYGOtMFu2cNbbwQQFT ? p = preview You should probably add the class .navbar-brand to the < ...
-
缩小导航栏后封面图片(Shrink navbar AFTER cover image)[2022-11-12]
借助jquery,您可以获得渲染图像的高度。 然后,您只需更改偏移参数。 这是一支笔: https : //codepen.io/anon/pen/KyQddz* { margin: 0; padding:0; box-sizing: border-b ... -
如果你这样做它应该工作。 .navbar-inner{ background-image: url('navbar-bg.png'); background-repeat: repeat-x; } 演示: 在这里 if you do it like this it should work. .navbar-inner{ background-image: url('navbar-bg.png'); background-repeat: repeat-x; } Demo: here
-
您必须解压缩ePub并解析元数据。 解析META-INF/container.xml并找到.OPF文件路径。 .OPF文件包含您需要的所有内容,带有名称cover的标签meta引用标签item ,这是图像封面。 您可以将此图像复制到某处并删除解压缩的文件。 PS:我是FolioReaderKit的创建者,一个用Swift 2编写的ePub Reader和Parser,这是我获得书籍封面FREpubParser.swift的地方 。 如果您不想处理XML,您可以使用它阅读ePub,它还将解析元数据并为您公开。 ...
-
更新小提琴 如果您知道图像只占据网站的顶部,则可以将文本对象放置在绝对位置,顶部为60%。 .text { position: absolute; top: 60%; } Updated fiddle If you know that the image willonly take up the top portion of the site, you can place the text object with an absolute position with the top at 6 ...