如何实现在iCarousel iPhone不同位置的图像删除function

我正在使用iCarousel如在这里显示的轮播types轮class轮播和实现删除function。我能够删除轮播中的图像,当我试图删除任何其他iomage在可见的屏幕它被移动到轮播框架和删除。
我需要从原来的位置删除图像。

- (void)loadView { [super loadView]; self.view.backgroundColor = [UIColor blackColor]; carousel = [[iCarousel alloc] initWithFrame:CGRectMake(-130,300, 320, 100)]; carousel.dataSource = self; carousel.delegate=self; carousel.type = iCarouselTypeLinear; carousel.scrollEnabled=YES; imageView=[[UIImageView alloc]initWithFrame:CGRectMake(60, 50, 200, 200)]; [self.view addSubview:imageView]; } - (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view { UIImage *image = [imagesArray objectAtIndex:index]; UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(0,0, 60, 60); [button setBackgroundImage:image forState:UIControlStateNormal]; [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; button.titleLabel.font = [button.titleLabel.font fontWithSize:50]; [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside]; button.tag=index; NSLog(@"tag is %d",button.tag); UIButton *deleteButton=[UIButton buttonWithType:UIButtonTypeRoundedRect]; deleteButton.frame= CGRectMake(50, -5, 20 ,20); UIImage *img = [UIImage imageNamed:@"close.png"]; [deleteButton setImage:img forState:UIControlStateNormal]; [deleteButton addTarget:self action:@selector(deleteButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; [button addSubview:deleteButton]; return button; } -(void)deleteButtonClicked:(int )sender { NSInteger currentIndex = carousel.currentItemIndex; [carousel removeItemAtIndex:currentIndex animated:YES]; } 

请帮我一下

在这里输入图像说明

您不应删除carousel.currentItemIndex中的项目,因为它不是与您单击的button相对应的项目,而只是当前居中的项目。

要获得您点击的button的正确的项目索引,请执行以下操作:

 -(void)deleteButtonClicked:(id)sender { //get the index of the item view containing the button that was clicked NSInteger index = [carousel indexOfItemViewOrSubview:sender]; //update the data model (always do this first) [imagesArray removeObjectAtIndex:index]; //remove item from carousel [carousel removeItemAtIndex:index animated:YES]; } 

尝试这个:

  NSInteger index = carousel.currentItemIndex; [carousel removeItemAtIndex:index animated:YES]; [imagesArray removeObjectAtIndex:index]; 

还添加这个:

 - (void)awakeFromNib { if (self) { //set up carousel data wrap = NO; } } 

或者让你的

carousel.type = iCarouselTypeCustom;

carousel.type = iCarouselTypeLinear;

然后执行这个: [carousel reloadData];