我有一个带有内部UIImageView的NSMutableArray。滚动动作后,我想更改某些UIImageView的图像,但不能。
这是我首先启动数组的方式:
NSMutableArray *dotsImageSliderList = [[NSMutableArray alloc]init];
for (int i=0; i<totalImages; i++)
{
UIImageView *dot =[[UIImageView alloc] initWithFrame:CGRectMake(distance,2,5,5)];
if (i==0)
{
dot.image=[UIImage imageNamed:@"dotselected.png"];
}
else
{
dot.image=[UIImage imageNamed:@"dotunselected.png"];
}
[dotsImageSliderList addObject:dot];
}
这是我检索UIImageView并尝试更改图像的方式:
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
[NSObject cancelPreviousPerformRequestsWithTarget:self];
NSInteger pagenumber = scrollView.contentOffset.x / scrollView.bounds.size.width;
NSLog(@"%zd", pagenumber);
if (pagenumber < [dotsImageSliderList count])
{
for (int i=0; i<[dotsImageSliderList count]; i++)
{
UIImageView *view = [dotsImageSliderList objectAtIndex:pagenumber];
if (i==pagenumber)
{
view.image=[UIImage imageNamed:@"dotselected.png"];
}
else
{
view.image=[UIImage imageNamed:@"dotunselected.png"];
}
}
}
}
我不知道为什么图像不改变。你能发现错误吗?
答案 0 :(得分:0)
在循环外初始化图像对象。
NSMutableArray *dotsImageSliderList = [[NSMutableArray alloc]init];
UIImage * selectedImage = [UIImage imageNamed:@"dotselected.png"];
UIImage * unselectedImage = [UIImage imageNamed:@"dotunselected.png"];
for (int i=0; i<totalImages; i++)
{
UIImageView *dot =[[UIImageView alloc] initWithFrame:CGRectMake(distance,2,5,5)];
if (i==0)
{
dot.image=selectedImage;
}
else
{
dot.image=unselectedImage;
}
[dotsImageSliderList addObject:dot];
}
答案 1 :(得分:0)
超级愚蠢的错误。我必须更改此行:
UIImageView *view = [dotsImageSliderList objectAtIndex:pagenumber];
对此:
UIImageView *view = [dotsImageSliderList objectAtIndex:i];