2

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

}
Bazinga
  • 2,456
  • 33
  • 76
  • I'm sorry I dont understand, you want to rotate an object with only one element in it? – Spire Jul 05 '12 at 07:48
  • hmmmm, something like that. or make the 1 element a duplicate then rotate it. – Bazinga Jul 05 '12 at 07:57
  • try if-else statement and when you have 1 element insert it twice, then you will have 2 elements and can rotate them. Just an idea – Spire Jul 05 '12 at 08:06
  • I don't know what you mean by that, could you show me? – Bazinga Jul 05 '12 at 08:31
  • `if([images count]==1){ [self.carousel insertItemAtIndex:0 animated:NO]; [self.carousel insertItemAtIndex:1 animated:NO]; }else{ }` it's just a guideline – Spire Jul 05 '12 at 13:32

1 Answers1

1

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.

sergio
  • 68,819
  • 11
  • 102
  • 123
  • well, I tried and it works; you will have some other issue preventing that from working. in any case, I modified a little bit the code so to make its integration with your code easier. – sergio Jul 05 '12 at 10:37
  • Ill post my code here like your code, can you kindly check it sir? – Bazinga Jul 05 '12 at 10:40
  • I'm really having problem with this part `numberOfViews`. I dunno if Im doing it right, if it should be `[images count]` or `carousel.numberOfItems`. please help sir. :( – Bazinga Jul 06 '12 at 02:31
  • I think that `[images count]` is ok. To move on, you should set a breakpoint in `numberOfItemsInCarousel` and `viewForItemAtIndex` and look at why they do not work as expected... maybe you have one image left in the carousel but `images` contains more elements, or I don't know what... – sergio Jul 06 '12 at 07:20
  • I was able to make it work, but it works only when view i loaded with 1 image, but when removing it doesn't, what can I do? – Bazinga Jul 06 '12 at 10:01
  • what do you mean: when removing it doesn't? if you remove the last image, what do you expect to see? – sergio Jul 06 '12 at 10:33
  • for example:when view LOADS(newly loaded) I have two images present, then I delete the result image then only 1 remains, it doesn't rotate. BUT,when view LOADS(newly loaded) then I only have 1 image present in my array, it rotates. – Bazinga Jul 06 '12 at 10:35
  • I have to admit that I have a pretty fuzzy view now... maybe you could post your full code for the delegate methods and how you deal with image deletion... – sergio Jul 06 '12 at 17:09
  • Your code seems ok. Could you try calling `loadUnloadViews` or `reloadData` after deleting a view when `carousel.numberOfItems == 1`? (or each time you delete). this could make it work, I hope... – sergio Jul 07 '12 at 20:14
  • the `loadUnloadView` can't be called on mine. only `reloadData` but, when reloadData is called, the last deleted item item is redisplay on the view. :( – Bazinga Jul 09 '12 at 02:15
  • Then it seems, that you did not update your data source. I think the only chance you have to fix this is by debugging into those methods and see what happens in actuality. Sorry I can't help further... – sergio Jul 09 '12 at 09:00
  • Thank you for the help. here is one of my bounty question, if you want to just take a look. http://stackoverflow.com/questions/11200584/nsmutablearray-difficulty – Bazinga Jul 09 '12 at 09:01