gpt4 book ai didi

iphone - 如何删除 iCarousel 中的重复项

转载 作者:行者123 更新时间:2023-11-29 13:24:25 24 4
gpt4 key购买 nike

https://github.com/nicklockwood/iCarousel/issues/278

我将使用 iCarousel 线性显示图像。这些图像将被动态获取。假设有 2 个图像。但是当我更新 iCarousel 时,背景中也有 2 个图像,如下图。


在调试代码时,我发现在我调用reloadData 函数后,viewForItemAtIndex 函数被调用了2 次。

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view



所以iCarousel中有4张图片。

如何防止被调用2次?

请尽快帮我解决这个问题。


- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIImageView *puzzlePhoto = nil;
UIImageView *puzzleBG = nil;
UILabel *label = nil;

if (view == nil)
{
view = [[UIImageView alloc] init];
view.frame = CGRectMake(0, 0, 128, 112);
view.backgroundColor = [UIColor clearColor];
view.layer.doubleSided = NO;
puzzlePhoto = [[UIImageView alloc] initWithFrame:CGRectMake(46, 48, 40, 40)];

//! get friend's photo
FForesight *foresightCurrent = (carousel == self.icGameOnwhoAREus) ? [controller.gameManager.m_arrayStartedForesights objectAtIndex:index] : [controller.gameManager.m_arrayEndedForesights objectAtIndex:index];
UIImage *imgPuzzle = [foresightCurrent getPuzzleImage];

if (imgPuzzle)
puzzlePhoto.image = imgPuzzle;
else
puzzlePhoto.image = [UIImage imageNamed:@"puzzle background_small.png"];

puzzleBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 128, 112)];
if (((foresightCurrent.m_intStage & FForesightStage_First) && !foresightCurrent.m_FCreatedByFriend) ||
((foresightCurrent.m_intStage & FForesightStage_Second) && foresightCurrent.m_FCreatedByFriend))
puzzleBG.image = [UIImage imageNamed:@"multi_game_one_item_bg_me.png"];
else
puzzleBG.image = [UIImage imageNamed:@"multi_game_one_item_bg.png"];

//! get friend name
FUser *userFriend = foresightCurrent.m_userFriend;
label = [[UILabel alloc] initWithFrame:CGRectMake(32, 32, 68, 16)];
label.text = userFriend.m_strName;
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont boldSystemFontOfSize:10];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor colorWithRed:205.0f/256 green:55.0f/256 blue:2.0f/255 alpha:1];

[view addSubview:puzzlePhoto];
[view addSubview:puzzleBG];
[view addSubview:label];
}

return view;
}

最佳答案

问题几乎可以肯定是由于 View 回收不正确造成的。 View 与不同的索引重复使用,您永远不应该在 View 创建代码中设置特定于索引的属性。以下是设置项目 View 的正确方法:

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIImageView *puzzlePhoto = nil;
UIImageView *puzzleBG = nil;
UILabel *label = nil;

const NSInteger puzzlePhotoTag = 1;
const NSInteger puzzleBGTag = 2;
const NSInteger labelTag = 3;

if (view == nil)
{
//*************************************************
//do setup that is the same for every item view here
//*************************************************

view = [[UIImageView alloc] init];
view.frame = CGRectMake(0, 0, 128, 112);
view.backgroundColor = [UIColor clearColor];
view.layer.doubleSided = NO;

puzzlePhoto = [[UIImageView alloc] initWithFrame:CGRectMake(46, 48, 40, 40)];
puzzlePhoto.tag = puzzlePhotoTag;
[view addSubview:puzzlePhoto];

puzzleBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 128, 112)];
puzzleBG.tag = puzzleBGTag;
[view addSubview:puzzleBG];

label = [[UILabel alloc] initWithFrame:CGRectMake(32, 32, 68, 16)];
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont boldSystemFontOfSize:10];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor colorWithRed:205.0f/256 green:55.0f/256 blue:2.0f/255 alpha:1];
label.tag = labelTag;
[view addSubview:label];
}
else
{
//get references to subviews
puzzlePhoto = (UIImageView *)[view viewWithTag:puzzlePhotoTag];
puzzleBG = (UIImageView *)[view viewWithTag:puzzleBGTag];
label = (UILabel *)[view viewWithTag:labelTag];
}

//*************************************************
//do setup that is different depending on index here
//*************************************************

//! get friend's photo
FForesight *foresightCurrent = (carousel == self.icGameOnwhoAREus) ? [controller.gameManager.m_arrayStartedForesights objectAtIndex:index] : [controller.gameManager.m_arrayEndedForesights objectAtIndex:index];
UIImage *imgPuzzle = [foresightCurrent getPuzzleImage];

if (imgPuzzle)
puzzlePhoto.image = imgPuzzle;
else
puzzlePhoto.image = [UIImage imageNamed:@"puzzle background_small.png"];

if (((foresightCurrent.m_intStage & FForesightStage_First) && !foresightCurrent.m_FCreatedByFriend) || ((foresightCurrent.m_intStage & FForesightStage_Second) && foresightCurrent.m_FCreatedByFriend))
puzzleBG.image = [UIImage imageNamed:@"multi_game_one_item_bg_me.png"];
else
puzzleBG.image = [UIImage imageNamed:@"multi_game_one_item_bg.png"];

//! get friend name
FUser *userFriend = foresightCurrent.m_userFriend;
label.text = userFriend.m_strName;

return view;
}

至于滚动惯性问题,如果您在应用此修复后仍然看到该问题,请在 github 页面上提交单独的错误报告。

关于iphone - 如何删除 iCarousel 中的重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13494339/

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