ios - 索引 iCarousel 中 UIView 的多选

标签 ios objective-c xcode uiview icarousel

因此,当我单击该 View 中的按钮时,我想在 iCarousel 中突出显示特定的 UIView,并且我希望能够在轮播中突出显示任意多个 View 。所以我有一个按钮在 View 中设置 UIImageview 的 alpha 我遇到的问题是虽然它知道 View 的索引我不知道如何调用相应的索引该 View 中的 UIImageview。所以我在自定义 nib 中设置了 UIImageview 并且我用这段代码设置了 iCarousel 的 View :

根据@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;
}

然后我在选择特定 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];
}

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

最佳答案

它的工作方式类似于表格 View 。您需要的是所选项目的模型。这需要比任何给定的轮播 View 更长久。所以添加一个 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];

最后一步是在配置 View 时对选择状态使用react:

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

编辑 - 部分问题似乎是创建然后获取轮播的 subview 。 UIView 提供了一个有用的标记属性,但发布的代码会出现一些错误。以下是如何使用标签:

如果您在 IB 中创建 ImageView ,请使用属性检查器为其添加标签。在这里,我给 View 赋予了 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.

关于ios - 索引 iCarousel 中 UIView 的多选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24148435/

相关文章:

ios - SIGABRT 使用 indexPath.row 访问 NSArray

objective-c - Xcode 无法发出预编译 header ?

ios - 如何在 Xcode 中实现图像数组?

svn - 在 XCode 中使用 SVN

ios - 了解函数中的快速代码元组复合

ios - 在其图像上显示 UIBarButton 标题

ios - 使用谷歌帐户登录,使用电子邮件,然后为应用程序 iOS 创建密码

ios - 在同一个 POST 中发送 NSData 和 NSString

objective-c - View 应仅显示为纵向模式

iphone - ASIWebPageRequest:始终以NSISOLatin1StringEncoding进行响应