• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 如何确保更改的 collectionview 单元格出现在手机屏幕的中心?

[复制链接]
菜鸟教程小白 发表于 2022-12-11 17:47:29 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

场景:我有一个每 15 秒重新加载一次的 Collection View 。此 Collection View 中的单元格数量没有限制,但是一次只能突出显示一个单元格。只有一个部分,collectionview 水平滚动。我需要确保突出显示的单元格始终位于手机屏幕的中央。例如,如果突出显示第 24 个单元格,则必须一直滚动直到找到它,这将是一种糟糕的用户体验。但是,当 Collection View 在另外 15 秒内重新加载时,可能会突出显示一个完全不同的单元格。

See bottom portion of the image for a better idea of highlighted and unhighlighted cells.

这是我尝试过的与突出显示的单元格的索引路径相关的想法,并确保它位于手机屏幕的中心。

- (UICollectionViewCell *)collectionViewUICollectionView *)collectionView cellForItemAtIndexPathNSIndexPath *)indexPath {

  EZPlayerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier"layerCellID" forIndexPath:indexPath];

  NSDictionary *rowData = [[_gameBoardDictionary objectForKey"players"] objectAtIndex:indexPath.row];

  if ([[rowData objectForKey"possession"] integerValue] == 1) {
     cell.isHighlighted = YES;
  } else {
     cell.isHighlighted = NO;
  }

  if (cell.isHighlighted) {

     self.highlightedCellIndexPath = [self.collectionView indexPathForCell:cell];

  } else {

  }

  return cell;
}

- (void)viewDidLayoutSubviews {

  [self.collectionView scrollToItemAtIndexPath:self.highlightedCellIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];

}

我尝试了很多东西,但这应该为我的思路提供引用。任何帮助,将不胜感激。非常感谢!



Best Answer-推荐答案


请为 Collection View 的“contentSize”键路径添加一个观察者。

[self.collectionView addObserver:self forKeyPath"contentSize" options:NSKeyValueObservingOptionOld context:NULL];

然后您可以在 observeValueForKeyPath 方法中移动滚动代码。

- (void)observeValueForKeyPathNSString *)keyPath ofObjectid)object changeNSDictionary  *)changeDict contextvoid *)context
{
  [self.collectionView scrollToItemAtIndexPath:self.highlightedCellIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}

关于ios - 如何确保更改的 collectionview 单元格出现在手机屏幕的中心?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39940003/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap