ios - 滚动后 UICollectionView 不会保持选中状态
<p><p>当我选择一个单元格时,它会变成蓝色,这很好。然后,当我向上滚动并且单元格不在 View 中时,它会变回原始颜色。不确定如何解决这个问题。</p>
<pre><code>- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell=;
cell.backgroundColor=;
;
self.collectionView.allowsMultipleSelection=YES;
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *datasetCell =;
datasetCell.backgroundColor = ;
}
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *datasetCell =;
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>在您的第一种方法中,您实际上是在告诉 Collection View 选择它要求的任何单元格。相反,您应该检查您返回的单元格是否与所选单元格具有相同的路径,然后才给它颜色:</p>
<pre><code>- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell=;
if () // you need to create selectedIndexPath as a property
{
cell.backgroundColor=;
}
else
{
cell.backgroundColor=;
}
return cell;
}
</code></pre>
<p>然后:</p>
<pre><code>-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
self.selectedIndexPath = indexPath;
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 滚动后 UICollectionView 不会保持选中状态,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/34322802/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/34322802/
</a>
</p>
页:
[1]