国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

在TableVIew中搜索篩選:謂詞

2019-11-14 18:17:56
字體:
供稿:網(wǎng)友

在TableView中搜索,篩選出自己需要的內(nèi)容,需要用到iOS中的謂詞:NSPRedicate.謂詞的功能很想數(shù)據(jù)庫(kù)中的查詢語(yǔ)句,就是從數(shù)據(jù)集合中篩選出符合條件的對(duì)象,這讓我想起了在qt時(shí)遇到的正則表達(dá)式,不過當(dāng)時(shí)沒有好好學(xué).

 

方法步驟:

首先在表中加入搜索輸入框

UITextField *_textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 250, 30)];    _textField.borderStyle = UITextBorderStyleRoundedRect;//設(shè)置代理    _textField.delegate = self;//再次輸入時(shí)清空   _textField.clearsOnBeginEditing = YES;//設(shè)置return功能    _textField.returnKeyType = UIReturnKeyDone;//添加點(diǎn)擊事件    [_textField addTarget:self action:@selector(filter:) forControlEvents:UIControlEventEditingChanged];    self.navigationItem.titleView = _textField;

設(shè)置textfield的點(diǎn)擊事件

#pragma mark - UITextField Delegate- (BOOL)textFieldShouldReturn:(UITextField *)textField{    [textField resignFirstResponder];    return YES;}

重點(diǎn):textfield點(diǎn)擊事件

- (void)filter:(UITextField *)textField{    //當(dāng)輸入框中沒有輸入內(nèi)容時(shí),搜索結(jié)果與原數(shù)據(jù)不變    if ([textField.text length] == 0) {        self.fontsArray = _data;        [self.tableView reloadData];        return;    }    //設(shè)置謂詞內(nèi)容    NSString *regex = [NSString stringWithFormat:@"SELF LIKE[c]'%@*'", textField.text];    NSPredicate *predicate = [NSPredicate predicateWithFormat:regex];    //將篩選后的結(jié)果傳遞到元數(shù)據(jù)數(shù)組中    self.fontsArray = [_data filteredArrayUsingPredicate:predicate];    [self.tableView reloadData];}

 

 
 

上一篇:iOS-常見問題

下一篇:閉包(block)

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 沾化县| 广平县| 出国| 丰镇市| 蛟河市| 阜城县| 潞城市| 罗定市| 宁海县| 土默特左旗| 鹤庆县| 盖州市| 沅江市| 霍州市| 樟树市| 衡阳县| 江城| 海盐县| 长子县| 河北区| 河西区| 嘉义县| 马公市| 启东市| 肇源县| 岳西县| 关岭| 泉州市| 察哈| 江阴市| 长顺县| 黄大仙区| 边坝县| 连南| 大同市| 曲周县| 赤水市| 济宁市| 浦县| 五峰| 正阳县|