ios - 在 UICollectionView 中搜索和过滤单元格

标签 ios objective-c search filter uicollectionview

我有一个带有一堆单元格的 UICollectionView,所以我想用这个 View 完成两件事。

首先,我希望在顶部有一个搜索栏,可以根据用户的查询过滤单元格。我只见过使用 UITableView 实现的搜索栏,那么我该怎么做呢?

另外,我想要一个名为“过滤器”的按钮,单击该按钮时,会显示一个弹出式 View Controller ,其中包含一系列复选框及其值。因此,如果我用户选择复选框,一旦用户按下位于右上角的“完成”按钮,它将根据他们的检查过滤单元格。如果用户不决定过滤他的搜索,左上角还会有一个“取消”按钮。

我的 UICollectionView 的图片:

UICollectionview

我的代码

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"Cell";
    backpackIcons *item = _backpackItems[indexPath.row];
    NSString *photoURL = item.image_url;
    NSString *quality = item.quality;
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    UIImageView *itemImageView = (UIImageView *)[cell viewWithTag:100];
    itemImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:photoURL]]];
    [itemImageView setBackgroundColor:Rgb2UIColor(60, 53, 46)];
    if([[NSString stringWithFormat:@"%@", quality] isEqualToString:@"6"])
    {
        [itemImageView.layer setBorderColor:[Rgb2UIColor(125, 109, 0) CGColor]];
    }
    else if([[NSString stringWithFormat:@"%@", quality] isEqualToString:@"1"])
    {
        [itemImageView.layer setBorderColor:[Rgb2UIColor(77, 116, 85) CGColor]];
    }
    else if ([[NSString stringWithFormat:@"%@", quality] isEqualToString:@"3"])
    {
        [itemImageView.layer setBorderColor:[Rgb2UIColor(71, 98, 145) CGColor]];
    }
    else if ([[NSString stringWithFormat:@"%@", quality] isEqualToString:@"5"])
    {
        [itemImageView.layer setBorderColor:[Rgb2UIColor(134, 80, 172) CGColor]];
    }
    else if ([[NSString stringWithFormat:@"%@", quality] isEqualToString:@"11"])
    {
        [itemImageView.layer setBorderColor:[Rgb2UIColor(207, 106, 50) CGColor]];
    }
    else if ([[NSString stringWithFormat:@"%@", quality] isEqualToString:@"7"] || [[NSString stringWithFormat:@"%@", quality] isEqualToString:@"9"])
    {
        [itemImageView.layer setBorderColor:[Rgb2UIColor(112, 176, 74) CGColor]];
    }
    else if ([[NSString stringWithFormat:@"%@", quality] isEqualToString:@"8"])
    {
        [itemImageView.layer setBorderColor:[Rgb2UIColor(165, 15, 121) CGColor]];
    }
    else if ([[NSString stringWithFormat:@"%@", quality] isEqualToString:@"0"])
    {
        [itemImageView.layer setBorderColor:[Rgb2UIColor(178, 178, 178) CGColor]];
    }
    else if ([[NSString stringWithFormat:@"%@", quality] isEqualToString:@"13"])
    {
        [itemImageView.layer setBorderColor:[Rgb2UIColor(56, 243, 171) CGColor]];
    }
    else
    {
        [itemImageView.layer setBorderColor:[Rgb2UIColor(170, 0, 0) CGColor]];
    }
        [itemImageView.layer setBorderWidth: 1.0];

    return cell;
}

最佳答案

我已经破解了一个搜索和过滤 UICollectionView 的简单示例,您可以在这里下载代码:https://github.com/kambala-decapitator/uicollectionview-search-filter .

我决定将搜索栏添加到导航栏,但您可以自由地将其作为 subview 添加到 Collection View (并在需要时调整 contentOffset 属性)或只是查看 Controller 的 View 。此外,也许您希望将 Filters 显示为一个简单的模态视图 Controller ,从编码的角度来看这更容易一些:)

请注意,在实际代码中,您应该将 UICollectionViewCell 子类化,而不是像我一样使用 subviews hack :)

timothykc已经提供了一些实现基础。如果有什么不清楚的地方,请随时询问。

关于ios - 在 UICollectionView 中搜索和过滤单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24119738/

相关文章:

iphone - iPhone 中的 iAd 问题

ios - 两个日期之间经过的天数总是相差一天

ios - __NSCFConstantString objectAtIndex :]: unrecognized selector sent to instance

MySQL 全文搜索没有匹配项

C# 循环遍历注册表以搜索包含特定文本的键

ios - 可变数组 iOS 中的最大元素

iOS:使用另一个类中定义的 UITableViewCell 中按钮的选定状态填充数组

ios - 如何使用 "appname://openApp?"在 iOS 中打开 App?

ios - 如何创建固定数量的 UITableViewCell,如果数据为空,则不应显示行

android - 禁用 SearchView