ios - UICollectionview 在滚动时更改选择/禁用选择 - iOS
<p><p>我在 uicollectionview 单元格上填充数据并选择和取消选择,一切正常,但是当我开始滚动时,有时选择不存在,有时选择随单元格而变化。以下是代码,不胜感激。</p>
<pre><code>- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
cell = (BYOCollectionCell *);
cell.vSelectionView.hidden = YES;
cell.vSelectionView.backgroundColor = customLightGreenColor;
;
pizzaInfo *pizzainfo= [init];
pizzainfo = ;
if (pizzainfo._bIsSelected)
{
cell.vSelectionView.hidden = NO;
}
else
{
cell.vSelectionView.hidden = YES;
}
//label customization
return cell;
}
</code></pre>
<p><strong>DidselectItem</strong> </p>
<pre><code>- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
cell = (BYOCollectionCell *);
pizzaInfo *pizzaInfoCellData = ;
byoPizzaInfo = ;
if ( pizzaInfoCellData._bIsSelected)
{
cell.vSelectionView.hidden = NO;
pizzaInfoCellData._bIsSelected = NO;
;
}
else
{
cell.vSelectionView.hidden = YES;
pizzaInfoCellData._bIsSelected = YES;
// deltaSelection:(pizzaInfo *)selectedItem
;
if (self._IsNotifiable) {
;
}
}
;
}
</code></pre>
<p><code>collectionViewCell</code> 的更多内容在 <code>tableViewCell 中。</code></p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>在您的 <code>cellForItemAtIndexPath</code> 中,您已经添加了隐藏和显示所选 View 的条件,因此您需要像这样更改您的 <code>didSelectItemAtIndexPath</code></p>
<pre><code>- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
pizzaInfo *pizzaInfoCellData = ;
if (pizzaInfoCellData._bIsSelected)
{
;
}
else
{
;
}
pizzaInfoCellData._bIsSelected = !pizzaInfoCellData._bIsSelected
;
}
</code></pre>
<p><strong>注意:-</strong> 类名总是以大写字母开头,所以如果你将类名 <code>pizzaInfo</code> 更改为 <code>PizzaInfo</code>,它的建议获得良好的编码指南。</p></p>
<p style="font-size: 20px;">关于ios - UICollectionview 在滚动时更改选择/禁用选择 - iOS,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/38419546/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/38419546/
</a>
</p>
页:
[1]