菜鸟教程小白 发表于 2022-12-11 17:47:29

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


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

<p> <a href="http://i.stack.imgur.com/eZOT9.png" rel="noreferrer noopener nofollow">See bottom portion of the image for a better idea of highlighted and unhighlighted cells.</a> </p>

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

<pre><code>- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

EZPlayerCollectionViewCell *cell = ;

NSDictionary *rowData = [ objectAtIndex:indexPath.row];

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

if (cell.isHighlighted) {

   self.highlightedCellIndexPath = ;

} else {

}

return cell;
}

- (void)viewDidLayoutSubviews {

;

}
</code></pre>

<p>我尝试了很多东西,但这应该为我的思路提供引用。任何帮助,将不胜感激。非常感谢!</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>请为 Collection View 的“contentSize”键路径添加一个观察者。</p>

<pre><code>;
</code></pre>

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

<pre><code>- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary*)changeDict context:(void *)context
{
;
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何确保更改的 collectionview 单元格出现在手机屏幕的中心?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/39940003/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/39940003/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何确保更改的 collectionview 单元格出现在手机屏幕的中心?