2012-02-15 75 views
0

我试图反映添加到我的icarousel。首先我的代码是这样的,它正在工作赖特。增加反射iCarousel

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index  reusingView:(UIView *)view 
{ 
UIButton* button = (UIButton *)view; 
if (button == nil) 
{ 
    //no button available to recycle, so create new one 
    UIImage *image = [arrKitapKapaklari objectAtIndex:index]; 
    button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    button.frame = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height); 
    [button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal]; 
    [button setBackgroundImage:image forState:UIControlStateNormal]; 
    button.titleLabel.font = [button.titleLabel.font fontWithSize:50]; 
    [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside]; 
} 


//set button label 
[button setTitle:[NSString stringWithFormat:@"%i", index] forState:UIControlStateNormal]; 

return button; 

}

改变后,我的代码变成这个样子:出现

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


UIButton* button = nil; 
if (button == nil) 
{ 

    view = [[[ReflectionView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 200.0f)] autorelease]; 
    //no button available to recycle, so create new one 
    UIImage *image = [arrKitapKapaklari objectAtIndex:index]; 
    button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    button.frame = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height); 
    [button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal]; 
    [button setBackgroundImage:image forState:UIControlStateNormal]; 
    button.titleLabel.font = [button.titleLabel.font fontWithSize:50]; 
    [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside]; 
     [view addSubview:button]; 
} 



//set button label 
[button setTitle:[NSString stringWithFormat:@"%i", index] forState:UIControlStateNormal]; 
[view update]; 
return view; 

}

反思。但问题是当我移动旋转木马视图时,图像流动不正确。有时会出现另一个轮播视图。

你知道是什么原因造成的吗?

+0

为什么在设置'button = nil;'之前有'if(button == nil)'?你永远不会运行该块中的代码。 – 2012-02-15 16:20:16

+0

其实他总是*要去运行它 - 但它仍然是错误的 - 请参阅下面的答案。 – 2012-02-16 11:44:54

+2

我是一个女性:) – 2012-02-16 13:18:57

回答

0

我用反射建于他们的图像...不需要做任何的代码方式..:对

+0

但事情是我从Web服务获取图像。所以我不知道如何来填充动态画面与反思 – 2012-02-15 19:21:00

+0

那么它似乎是合理的.. – 2012-02-16 06:56:13

1

代码应该是:

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


UIButton* button = nil; 
if (view == nil) 
{ 
     //no view available to recycle, so create new one 
    view = [[[ReflectionView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 200.0f)] autorelease]; 

    button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    button.frame = view.bounds; 
    [button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal]; 
    button.titleLabel.font = [button.titleLabel.font fontWithSize:50]; 
    [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside]; 
      button.tag = 99; 
     [view addSubview:button]; 
} 
else 
{ 
    button = (UIButton *)[view viewWithTag:99]; 
} 

//set button label 
[button setTitle:[NSString stringWithFormat:@"%i", index] forState:UIControlStateNormal]; 

UIImage *image = [arrKitapKapaklari objectAtIndex:index]; 
[button setBackgroundImage:image forState:UIControlStateNormal]; 

[view update]; 
return view; 
} 
+0

遗憾它没有工作。也许我应该解释清楚。首先我将图像添加到名为arrKitapKapaklari的数组中。然后我将它们替换为我从web服务获得的图像。问题在那里。图像在流动时有阴影。如果我不创建反射视图,它可以正常工作。但我需要反思。我认为当它尝试添加子视图时会出现问题。这似乎是双重视角。我为我可怕的英语感到抱歉。谢谢.. – 2012-02-15 19:30:37

+0

我已经更新了我的答案。我不认为你已经理解了整个重用机制。它像一个表视图一样工作 - if(view == nil)部分中的任何内容只发生一次,因此每次显示视图时需要更新的任何内容都必须超出if语句。背景加载的东西是一个单独的问题。 – 2012-02-16 11:41:52

0

我发现我错了。我在这里加载图片: - (无效){getImagesWithThread NSAutoreleasePool *池; pool = [[NSAutoreleasePool alloc] init];

//for(int i=0;i<bookCount;i++){ 
for(int i=0;i<bookCount;i++){ 

    NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:  [arrBookImages objectAtIndex:i]]]; 
    UIImage *image = [[UIImage imageWithData: imageData] retain]; 

    image = [self scaleToSize:CGSizeMake(140, 200) withImage:image]; 

    if(image!=nil) [arrBookImages replaceObjectAtIndex:i withObject:image]; 



    [image release]; } 

[carousel reloadData];

[pool drain];

} 当我在后台执行此操作时,它没有正确重新加载。如果我正常运行,它就可以工作。但我想加载它们在一个线程中。我的意思是首先我想设置图像并从url中替换它们的图像。但我希望他们一个一个出现。你有什么主意吗?

+1

是的,当您加载新图像时,您需要更新传送带中相应的项目。我建议每次在后台加载图像时调用[carousel reloaditemAtIndex:animated:]。确保你在主线程上调用它,因为在后台线程上更新UI是不安全的。您可以使用performSelectorOnMainThread从背景线程调用主线程上的更新。我将添加一个背景加载示例到iCarousel的下一个版本,因为这是一个常见问题。 – 2012-02-16 11:43:56