2

我正在iCarousel用作老虎机,对于那些不知道iCarousel它是UIScrollviewwith(分页,滚动视图)的人。所以在我的应用程序中,我滚动它,然后当它停止时它会显示图像(结果)3秒,然后弹出一个按钮,如果你按下按钮,它将删除视图(图像结果)然后再次旋转。

这是我删除轮播视图的方法:

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

但是当carousel.numberOfItems剩下 1 个项目时,它不会滚动,而是卡在那里。

所以我做了一种方法让它滚动,即使它只剩下 1 个项目(索引)。

我用最后一个索引插入了它,所以我尝试了这个:

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

(但不起作用)

然后我尝试了另一个:

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

(仍然不起作用)

还有这个:

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

(仍然没有运气)

我做错了吗?或者还有什么其他方法?或者我可以复制最后一个视图吗?谢谢您的帮助。

编辑: 方法

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

删除方法:

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

}
4

1 回答 1

2

你能做的就是做

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

总是返回一个 >1 的值(例如 2);然后确保

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

总是为您返回的最少元素返回正确的视图numberOfItemsInCarousel

例如。

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

您还应该确保永远不会删除最后一个元素,并在上面的代码中添加一些保护来管理没有元素留下的情况。

于 2012-07-05T09:03:31.217 回答