iOS谓词做出自定义搜索效果

2023-05-16

- (void)viewDidLoad {

    [super viewDidLoad];

//第一个数组为总库存,第二个数组用来存储搜索后的结果

    _array=[NSMutableArray array];

    _arrayb=[NSMutableArray array];

    [_array addObject:@"1df"];

    [_array addObject:@"2fbjdb"];

    [_array addObject:@"3dbfjg"];

    [_array addObject:@"nnnnn"];

    [_array addObject:@"uuuuu"];

    

    [self creatTableview];

}


//tableview展示搜索前和搜索后的结果

-(void)creatTableview{

    _TableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];

    _TableView.dataSource=self;

    _TableView.delegate=self;

    [_TableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

    [self.view addSubview:_TableView];


}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



#pragma mark tableview代理事件

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 70;

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 60;

}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    if ([_arrayb count]==0) {

     return [_array count];

    }else{

        return [_arrayb count];

    }

   

}




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 1;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    

    if ([_arrayb count]==0) {

           cell.textLabel.text=_array[indexPath.row];

    }else{

          cell.textLabel.text=_arrayb[indexPath.row];

    }

    return cell;

    

}


//头视图里放textfield用来做搜索框(非系统自带)

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{

    UIView *view=[[UIView alloc]init];

    _textF=[[UITextField alloc]init];

    _textF.placeholder=@"搜索供应商";

    _textF.delegate=self;

    _textF.backgroundColor=[UIColor whiteColor];

    [_textF addTarget : self action : @selector (didDone:) forControlEvents : UIControlEventEditingChanged ];

    _textF.frame=CGRectMake(30, 10, self.view.bounds.size.width-60, 50);

    [view addSubview:_textF];

    return view;

}


//谓词过滤,实时搜索,刷新

-(void)didDone:(UITextField *)sender

{

    [_arrayb removeAllObjects];

  NSPredicate* predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@",_textF.text];

    for  (NSString *str in _array) {

        if ([predicate evaluateWithObject:str]) {

            if ([_arrayb containsObject:str]) {

                

            }else{

            [_arrayb addObject:str];

            }

        }

    }

    if ([_arrayb count]!=0) {

     [_TableView reloadData];

    }

    if ([_textF.text isEqualToString:@""]) {

        [_arrayb removeAllObjects];

        [_TableView reloadData];

    }

}





转载于:https://my.oschina.net/applexu/blog/620130

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

iOS谓词做出自定义搜索效果 的相关文章

  • BUG - 在 IOS 中没有选择标签的完成按钮

    我正在使用最新的离子并有一个简单的选择标签
  • 创建类似于邮件应用程序菜单的 iPhone 弹出菜单

    当您想要回复消息时 我想创建一个类似于邮件应用程序中的弹出菜单 我在多个应用程序中看到过这一点 所以我不确定框架中是否内置了某些内容或一些示例代码 在 Swift 中创建操作表 代码已使用 Swift 5 进行测试 从 iOS 8 开始 U
  • 部署目标是什么意思?

    这是我假设的一个非常简单的问题 有人可以告诉我部署目标是什么意思吗 如果我选择 iOS 10 是否意味着只有 iOS 10 的用户才能下载该应用程序 选择较低的部署目标是否不好 另外 继续部署目标 是否不建议在较低的部署目标上运行 假设您已
  • CLLocation Manager如何在一定距离后更新

    我正在使用 CLLocationManager didupdatelocations 如下所示 func locationManager manager CLLocationManager didUpdateLocations locati
  • 从数组中获取随机字符串[重复]

    这个问题在这里已经有答案了 我试图从数组 firstArray 中获取随机字符串并将其打印在 UILabel label 中 我似乎无法弄清楚并且出现错误 感谢您的帮助 我尝试搜索但找不到任何最新的教程 方法 import UIKit cl
  • 如何建立辅助NSSortDescriptor排序键?

    我已成功按排序键对数据进行排序lastName 但我想知道如何排序lastName 然后由firstName 这是我用来排序的代码lastName NSSortDescriptor sortDescriptor NSSortDescript
  • UITableview 中的水平和垂直滚动[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 I want to make a lineup for a festival You can see what I want to a
  • 自定义字体显示在 IB 中,但不显示在模拟器中

    我已经设置了一个UITextView and a UILabel使用自定义字体 它是垂直镜像的蒙古文字体 但我还添加了英文文本 以便您可以看到效果 这些文字显示在 Interface Builder 中 但在模拟器中大部分字符都在UITex
  • 由 Xcode 机器人运行时,在文本视图中键入文本时 UI 测试失败

    我有以下 XCTest UI 测试 将文本键入文本视图 let textView app textViews elementBoundByIndex 0 textView tap textView typeText Hello world
  • 如何在IOS中的UIStackView中设置权重

    UIStackView与安卓类似LinearLayout但我不知道如何设置子视图的权重 假设我有一个垂直的UIStackView and 3 UIImageView就在里面 我想连续设置权重3 6 1UIImageViews 我怎么做 UI
  • UICollectionView 自动滚动到 IndexPath 处的单元格

    在加载集合视图之前 用户设置集合视图数组中的图像数量 所有单元格都不适合屏幕 我有 30 个单元格 但屏幕上只有 6 个 问题 如何在加载 UICollectionView 时自动滚动到具有所需图像的单元格 新的 编辑过的答案 将其添加到v
  • 无法构建 Saurik 的 ldid 实用程序

    当我执行此命令 make sh 时 我遇到这些错误 构建用于越狱调整开发的 ldid 实用程序 Bilals Mac ldid billy make sh g arch i386 arch x86 64 arch ppc arch armv
  • Swift Generics 在使用继承时不会实例化泛型

    我有课Alpha and Berry class Alpha class Berry Alpha 我有一个使用继承及其泛型的函数 func myFunc
  • 网站在 iPhone 屏幕右侧显示空白区域

    我遇到问题http eiglaw com http eiglaw com iPhone 屏幕右侧显示约 25 像素宽的空白 边框 我在 stackoverflow 上研究了这个问题 这些帖子是相关的 但是当我尝试提供的各种解决方案时 我无法
  • 在 WKWebView 中禁用放大手势

    我正在寻找一种方法来禁用 WKWebView 的 iOS 实现上的 捏合缩放 放大手势 OS X 有一个 magnification BOOL 属性 但在 iOS 上似乎不可用 WKWebView h if TARGET OS IPHONE
  • 带操作按钮的颤动本地通知

    我在我的 flutter 项目中尝试了 flutter 本地通知插件 它在简单通知上工作正常 但我需要带有操作按钮的通知功能 请帮助我或建议我实现此功能 不幸的是 flutter local notifications 插件尚不支持操作按钮
  • 推送动画,没有阴影和停电

    我有一个简单的iOS NavigationController基于应用程序 二UICollectionViews 相继 如果元素打开 第一个合集 被点击时 第二集 将被打开 非常简单 重要的提示 Both UICollectionViews
  • ios水平居中约束问题?

    I am having hard time in learning constraints auto layout in iOS I have used any width any height I have a storyboard sc
  • 在 iOS 7 Safari 中,如何区分通过边缘滑动与后退/前进按钮的 popstate 事件?

    在 iOS 7 Safari 中 现在有两种后退 前进导航方式 使用底部的传统后退 前进按钮箭头或从屏幕边缘滑动 我正在使用动画在 ajax 应用程序中的页面之间进行转换 但如果用户通过边缘滑动进行导航 我不想触发该转换 因为这本身就是一个
  • PFQueryTableViewController 错误

    我正在遵循在线教程 使用 Parse 作为后端创建照片共享应用程序 我已经运行了两次教程 两次都从头开始创建应用程序 但在同一位置仍然出现相同的错误 我到处寻找解决方案 但仍然没有运气 我正在使用 PFQueryTableViewContr

随机推荐