首页 \ 问答 \ UICollectionView未正确更新 - 使用THStringyFlowLayout(UICollectionView is not updating properly - used THStringyFlowLayout)

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
更新时间:2024-03-27 14:03

最满意答案

借助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;
     }

相关问答

更多

相关文章

更多

最新问答

更多
  • Runnable上的NetworkOnMainThreadException(NetworkOnMainThreadException on Runnable)
  • C ++ 11 + SDL2 + Windows:多线程程序在任何输入事件后挂起(C++11 + SDL2 + Windows: Multithreaded program hangs after any input event)
  • AccessViolationException未处理[VB.Net] [Emgucv](AccessViolationException was unhandled [VB.Net] [Emgucv])
  • 计算时间和日期差异(Calculating Time and Date difference)
  • 以编程方式标签NSMutableAttributedString swift 4(Label NSMutableAttributedString programmatically swift 4)
  • C#对象和代码示例(C# objects and code examples)
  • 在python中是否有数学nCr函数?(Is there a math nCr function in python? [duplicate])
  • 检索R中列的最大值和第二个最大值的行名(Retrieve row names of maximum and second maximum values of a column in R)
  • 给定md5哈希时如何查找特定文件(How to find specific file when given md5 Hash)
  • Python字典因某些原因引发KeyError(Python Dictionary Throwing KeyError for Some Reason)
  • 如何让Joomla停止打开新标签中的每个链接?(How do I get Joomla to stop opening every link in a new tab?)
  • DNS服务器上的NS记录不匹配(Mismatched NS records at DNS server)
  • Python屏幕捕获错误(Python screen capture error)
  • 如何在帧集上放置div叠加?(How to put a div overlay over framesets?)
  • 页面刷新后是否可以保留表单(html)内容数据?(Is it possible to retain the form(html) content data after page refreshed?)
  • 使用iTeardownMyAppFrame和iStartMyAppInAFrame在OPA5测试中重新启动应用程序超时(Restart app within OPA5 test using iTeardownMyAppFrame and iStartMyAppInAFrame timed out)
  • 自动拆分文本内容到列(Automatically splitting text content into even columns)
  • 在r中的循环中将模型名称分配给gbm.step(assigning model names to gbm.step in loop in r)
  • 昆明哪里有电脑等级考试二级C培训?
  • C ++模板实例化,究竟是什么意思?(C++ template instantiation, what exactly does it mean?)
  • 帮助渲染来自fields_for的部分内容(Help to render a partial from fields_for)
  • 将url.action作为json对象返回mvc(return url.action as json object mvc)
  • 使用.BAT中的.application文件类型运行ac#Console App(Run a c# Console App with .application file type from a .BAT)
  • 将bindingRedirect添加到.Net标准库(Adding a bindingRedirect to a .Net Standard library)
  • Laravel版本升级会影响您的控制器吗?(Laravel version upgrade affects your controller?)
  • imaplib.error:命令SEARCH在状态AUTH中非法,只允许在SELECTED状态(imaplib.error: command SEARCH illegal in state AUTH, only allowed in states SELECTED)
  • 如何在eclipse debug impala前端
  • 如何通过Ajax API处理多个请求?(How to handle multiple requests through an Ajax API? [closed])
  • 使用Datetime索引来分析数据框数据(Using Datetime indexing to analyse dataframe data)
  • JS 实现一个菜单效果