0
votes

So I want to highlight a specific UIView in iCarousel when I click a button within that view and I want to be able to highlight as many views as I want within the carousel. So I have a button that sets the alpha of a UIImageview within the view the problem that I'm running into is although it knows the index of the view I don't know how to call the corresponding index of the UIImageview within that view. So I setup the UIImageview within a custom nib and I have this code setting up the iCarousel's view it:

UPDATED based on @danh answer

//.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;
}

Then I have this called when the specific view is being selected:

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

Which works but only allows me to select one item at a time and check it. I want to be able to select/unselect multiple items at a time.

1

1 Answers

1
votes

It works like a table view. You need is a model for selected items. This needs to outlive any given carousel view. So add a property NSMutableSet *selectedItems and initialize it with self.selected = [NSMutableSet set];

When the button is pressed, you want to add the current index to that set (does that button toggle selection? if so, then you want to add it if it's absent, or remove it if it's present).

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

The last step is to react to the selection state when you configure a view:

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

EDIT - Part of the problem seems to be creating and then later getting subviews of the carousel. UIView provides a tag property that will help, but the posted code makes a couple errors. Here's how to use tags:

If you create the image view in IB, give it a tag using the properties inspector. Here, I gave a view a tag of 32.

enter image description here

Now SOME_TAG in my suggestion above should be 32, so...

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

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