How to implement delete functionality for images in different positions in iCarousel iphone

旧城冷巷雨未停 提交于 2019-12-13 06:20:10

问题


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.


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/11241139/how-to-implement-delete-functionality-for-images-in-different-positions-in-icaro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!