索引iCarousel中的多个UIView选择

时间:2014-06-10 18:33:09

标签: ios objective-c xcode uiview icarousel

所以我想在iCarousel中突出显示特定的UIView,当我点击该视图中的按钮时,我希望能够在旋转木马中突出显示尽可能多的视图。所以我有一个按钮,在视图中设置UIImageview的alpha我遇到的问题虽然它知道视图的索引我不知道如何调用相应的该视图中UIImageview的索引。所以我在自定义笔尖中设置了UIImageview,我将此代码设置为iCarousel查看它:

根据@danh回答更新

//.h
@property (nonatomic, strong)NSMutableSet *selected;
//.m
- (void)viewDidLoad
 {
 self.selected = [NSMutableSet set];
}

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

    //create new view if no view is available for recycling
    if (view == nil)
    {
        view = [[[NSBundle mainBundle] loadNibNamed:@"ItemView" owner:self options:nil] lastObject];
}
 int chkTag = checkImg.tag;
checkImg = (UIImageView *)[checkImg viewWithTag:chkTag];

NSNumber *indexNum = [NSNumber numberWithInt:index];
// here's where the view state gets set
checkImg.alpha = ([self.selected member:indexNum])? 1.0 : 0.0;
  return view;
}

然后我在选择特定视图时调用它:

- (void)carouselCurrentItemIndexUpdated:(iCarousel *)carousel1{
    NSInteger indexInt = carousel1.currentItemIndex;
NSNumber *index = [NSNumber numberWithInt:indexInt];
if ([self.selected member:index]) {
    [self.selected removeObject:index];
} else {
    [self.selected addObject:index];
}

// now just reload that item, and let the viewForItemAtIndex
// take care of the selection state
[carousel reloadItemAtIndex:indexInt animated:NO];
}

哪个有效,但只允许我一次选择一个项目并进行检查。我希望能够一次选择/取消选择多个项目。

1 个答案:

答案 0 :(得分:1)

它就像一个表视图。您需要的是所选项目的模型。这需要比任何给定的轮播视图寿命更长。因此,添加property NSMutableSet *selectedItems并使用self.selected = [NSMutableSet set];

初始化它

按下按钮时,您想要将当前索引添加到该组中(该按钮是否切换选择?如果是,那么您希望在没有时添加它,或者如果它存在则将其删除)。

NSInteger indexInt = carousel1.currentItemIndex;
NSNumber *index = [NSNumber numberWithInt:indexInt];
if ([self.selected member:index]) {
    [self.selected removeObject:iIndex];
} else {
    [self.selected addObject:index];
}

// now just reload that item, and let the viewForItemAtIndex
// take care of the selection state
[carousel reloadItemAtIndex:indexInt animated:NO];

最后一步是在配置视图时对选择状态作出反应:

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

    if (view == nil) {
        view = [[[NSBundle mainBundle] loadNibNamed:@"ItemView" owner:self options:nil] lastObject];
    }

    // get the checkImg view however you do that already.  if you're confused about this
    // give it a tag when you create it, then find it like this:
    UIImageView *checkImg = (UIImageView *)[view viewWithTag:SOME_TAG];

    NSNumber *indexNum = [NSNumber numberWithInt:index];
    // here's where the view state gets set
    checking.alpha = ([self.selected member:indexNum])? 1.0 : 0.0;

  return view;
}

编辑 - 问题的一部分似乎是创建,然后获得轮播的子视图。 UIView提供了一个有用的标签属性,但是发布的代码会产生一些错误。以下是标签的使用方法:

如果在IB中创建图像视图,请使用属性检查器为其添加标记。在这里,我给了一个32的标签。

enter image description here

现在我的建议SOME_TAG应该是32,所以...

UIImageView *checkImg = (UIImageView *)[view viewWithTag:32];
NSLog(@"%@", checkImg);

// if this doesn't log an image view, then something is wrong.