0
votes

I am using iCarousel as shownh here with carousel type Liner Carousel and implemented delete functionalty .I am able to delete the image in carousel and when I attempt to delete any other iomage in the visible screen It is moved to carousel frame and deleted.
I need to delete the image from its original position.

- (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];
   
}

Please help me out.

enter image description here

2
delete from the array? and what do you mean by delete the image from its original position.Bazinga
- (BOOL)carouselShouldWrap:(iCarousel *)carousel { return NO; } I am already using this in my codeYoungwing
IT should delete when it is in its current position..But with my code it is scrolled to caroselframe position.Youngwing
Simply I able to edit or delete the image which is in firstposition of my visible screenYoungwing

2 Answers

2
votes

You shouldn't delete the item at the carousel.currentItemIndex because that is not the item corresponding to the button you clicked, that's just the currently centred item in the carousel.

To get the correct item index for the button you clicked, do this:

-(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];
}
1
votes

Try this:

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

add this also:

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

or make your

carousel.type = iCarouselTypeCustom;

to

carousel.type = iCarouselTypeLinear;

then implement this: [carousel reloadData];