gpt4 book ai didi

ios - 如何在 Objective-c 的 UICollectionView 中保留 didSelect 操作?

转载 作者:行者123 更新时间:2023-11-29 00:37:41 29 4
gpt4 key购买 nike

我有一个 UICollectionView,我从我的 cellForItemAtIndexPath 添加了 12 个单元格。当我滚动查看向下的单元格时,所有功能都被保留,但是当我再次向上滚动时,一些单元格不执行 didSelectItemAtIndexPath 中加载的功能。

我设置禁用了一些行。但不是我点击的行 为什么会是这样?可能是再利用电池的不良准备?

我正在尝试重用功能,但这只会影响单元格绘制错误或在另一个位置上,并且在 didSelectItemAtIndexPath 中添加的功能不起作用:

[self.myCollectionView reloadData];

[self.myCollectionView layoutIfNeeded];

NSArray *visibleItems = [self.myCollectionView indexPathsForVisibleItems];
NSIndexPath *currentItem = [visibleItems objectAtIndex:0];
NSIndexPath *nextItem = [NSIndexPath indexPathForItem:currentItem.item + 1 inSection:currentItem.section];
[self.myCollectionView scrollToItemAtIndexPath:nextItem atScrollPosition:UICollectionViewScrollPositionTop animated:YES];

当我滚动并点击一个单元格时,这并没有打开我的第二个 ViewController,我认为这个单元格正在获取一个错误的索引,该索引已被禁用。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

MyViewController *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCell" forIndexPath:indexPath];

switch ([indexPath row]) {
case 0:
titleCell= @"Title0";
detailCell=@"Detail0";
if([indexPath row]==2){
[cell setUserInteractionEnabled:NO];//Here I set disable Could be the problem caused by this??
}

[cell.image setHidden:YES];
cell.image.contentMode = UIViewContentModeScaleAspectFit;
break;
//Here other cases with the same structure

return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
[collectionView deselectItemAtIndexPath:indexPath animated:NO];
MySecondClass *secondClass = [self.storyboard instantiateViewControllerWithIdentifier:@"myVC"];
[self.navigationController pushViewController:secondClass animated:YES];
}

-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
//Here assign yellow background color to first row and white color to the second row
}

在我的单元格类中我添加了 prepareForReuse 但这不起作用...

- (void)prepareForReuse {
[super prepareForReuse];

[self removeFromSuperview];
[self setNeedsLayout];
}

最佳答案

问题出在这段代码上:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

MyViewController *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCell" forIndexPath:indexPath];

switch ([indexPath row]) {
case 0:
titleCell= @"Title0";
detailCell=@"Detail0";
if([indexPath row]==2){
[cell setUserInteractionEnabled:NO];//Here I set disable Could be the problem caused by this??
}

[cell.image setHidden:YES];
cell.image.contentMode = UIViewContentModeScaleAspectFit;
break;

return cell;
}

因为细胞是可以重复使用的。因此,您之前禁用的单元格(例如 cell0)将继续被重用。例如,当您滚动时,cell0 将变为 cell11。但是,其设置仍然处于禁用状态。

您需要添加一个 else 语句来删除禁用的设置,如下所示。

if([indexPath row]==2){
[cell setUserInteractionEnabled:NO];//Here I set disable Could be the problem caused by this??
}
else{
[cell setUserInteractionEnabled:YES];
}

关于ios - 如何在 Objective-c 的 UICollectionView 中保留 didSelect 操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40249818/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com