Search code examples
iphoneiosxcodensmutablearrayicarousel

UIScrollVIew View


I'm using iCarousel as a slot machine, for those who doesn't know iCarousel it is a UIScrollview with (paged, scrolling views). So in my app, I scroll it, then when it stops it shows the Image(Result) for 3 seconds, then a button pop-ups, if you press the button, it will delete the View(Image Result) then Rotates again.

Here is my way of deleting my carousel view:

    NSInteger CurrentImage = carousel.currentItemIndex;
    [carousel removeItemAtIndex:CurrentImage animated:YES];
    [images removeObjectAtIndex:CurrentImage];

But then when the carousel.numberOfItems has 1 item left, it doesnt scroll, instead its just stuck there.

So I made a way to make it scroll even it has only 1 Item(index) left.

I inserted it with the last index, so I tried this:

[self.carousel insertItemAtIndex:0 animated:NO];

(but doesnt work)

then I tried another:

int lastObject = [images objectAtIndex: ([images count]-1)]
[self.carousel insertItemAtIndex:lastObject animated:NO];

(still doesnt work)

and also this:

int count = [images count];
[images objectAtIndex:count - 1]; 

(still no luck)

Am I doing wrong? or What are other ways? Or can I just duplicate the last View? Thanks for the help.

EDIT: Methods

     - (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
    {
        if ([images count] == 1 || [self.carousel numberOfItems] == 1)
            return 2;

        return [images count];
    }

    - (NSUInteger)numberOfVisibleItemsInCarousel:(iCarousel *)carousel
    {
        //limit the number of items views loaded concurrently (for performance reasons)
        return 7;
    }


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

        if (index >= [images count] || index >= [carousel numberOfItems]) {
            index = 0;
        }

        NSDictionary *obj = [images objectAtIndex:index];
        view = [[UIImageView alloc] initWithImage:[obj objectForKey:@"image"]];
        view.tag = index;

        return view;
    }


    - (NSUInteger)numberOfPlaceholdersInCarousel:(iCarousel *)carousel
    {
        //note: placeholder views are only displayed on some carousels if wrapping is disabled
        return 0;
    }


    - (CGFloat)carouselItemWidth:(iCarousel *)carousel
    {
        //usually this should be slightly wider than the item views
        return 400;
    }

 - (BOOL)carousel:(iCarousel *)carousel shouldSelectItemAtIndex:(NSInteger)index{
    return 5;  
}   
    - (BOOL)carouselShouldWrap:(iCarousel *)carousel
    {
        //wrap all carousels
        return wrap;
    }

Delete Method:

-(void) deleteItem{
    //Removes the object chosen 
    if (carousel.numberOfItems >= 1)
    {   
        NSInteger index = carousel.currentItemIndex;
        [carousel removeItemAtIndex:index animated:YES];
        //[images removeObjectAtIndex:index];
        //[images replaceObjectAtIndex:index withObject:[NSNull null]];
    } 

}

Solution

  • What you could do is make

    - (NSUInteger)numberOfItemsInCarousel:(iCarousel*)carousel;
    

    return always a value >1 (eg. 2); then make sure that

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

    always returns a correct view for the minimum number of elements you return from numberOfItemsInCarousel.

    Eg.

    - (NSUInteger)numberOfItemsInCarousel:(iCarousel*)carousel {
       if (numberOfViews == 1)
          return 2;
       return numberOfViews;
    }
    
    
    - (UIView*)carousel:(iCarousel*)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView*)view {
    
      if (index >= numberOfViews) {
          index = 0;
      }
    
      NSDictionary *obj = [images objectAtIndex:index];
      view = [[UIImageView alloc] initWithImage:[obj objectForKey:@"image"]];
      view.tag = index;
      return view;
    }
    

    You should also make sure that the last element is never deleted of add some guards in the code above to manage the case when no elements are left.