iOS – tableView类型的筛选框实现

栏目: IOS · 发布时间: 4年前

内容简介:引言:在iOS开发中,条件筛选框基本出现在每一个app中,今天我们就来了解一下条件筛选框效果图显而易见,我们的这个筛选框是通过tableview来实现的,所以我们自定义了一个继承自UIView的子类synthesizeMenuPopView

引言:在iOS开发中,条件筛选框基本出现在每一个app中,今天我们就来了解一下条件筛选框

iOS – tableView类型的筛选框实现

效果图

显而易见,我们的这个筛选框是通过tableview来实现的,所以我们自定义了一个继承自UIView的子类synthesizeMenuPopView

synthesizeMenuPopView.h

typedef void (^synthesizeMenuPopBlock)(NSString *cellTitle);
@interface synthesizeMenuPopView : UIView<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataArray ;
@property (nonatomic, copy) NSString *CellText ;
@property(nonatomic,copy)synthesizeMenuPopBlock block ;
@end

我们这里定义:

1 . 一个匿名函数 – block 用于传值

2 . 一个数组 – dataArray 用于tableview上展示的数据

3 . 一个字符串 – CellText 用于判断当前选中的文字和显示的文字效果

synthesizeMenuPopView.m

-(instancetype)initWithFrame:(CGRect)frame{    
if (self == [super initWithFrame:frame]) {
        self.backgroundColor = [kTextFieldColor colorWithAlphaComponent:0.6] ; 
        // kTextFieldColor是我自己定义的宏,自行修改一下
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, self.height)] ;
        tableView.delegate = self ;
        tableView.dataSource = self ;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone ;
        

[self addSubview:tableView]

;         self.tableView = tableView ;          // 将self.tableView指向tableView所在的地址        }         return self ; } //这里重写dataArray 的set方法,当每次给dataArray赋值的时候都会调用该方法,从而修改tableview的高度 -(void)setDataArray:(NSMutableArray *)dataArray{     _dataArray = dataArray ;     self.tableView.height = _dataArray.count * 85 * kFitWithWidth ; } //设置tableView的代理方法 – (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{     return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{     return self.dataArray.count ; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{     return 85 * kFitWithWidth ; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{     static NSString * const cellID = @”cellID” ;     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID] ;         if (cell == nil) {         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID] ;     }     cell.textLabel.text = self.dataArray[indexPath.row];         if ([self.CellText isEqualToString:cell.textLabel.text]) {         cell.textLabel.textColor = RGB(247, 97, 76) ;     }else{         cell.textLabel.textColor = [UIColor blackColor] ;     }     cell.textLabel.font = [UIFont systemFontOfSize:10] ;         return cell ; } //tableView的点击事件 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{     [tableView deselectRowAtIndexPath:indexPath animated:YES];         // 获取选中cell     UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];         // 改变选中cell的 标题颜色     cell.textLabel.textColor = RGB(247, 97, 76);         // 传出选中cell的 标题     if (self.block != nil) {             self.block(cell.textLabel.text);     }         // 刷新tableView

[self.tableView reloadData]

;         // 选中后隐藏     self.hidden = YES;         return ; } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{     self.hidden = ! self.hidden ; }

ViewController.m中

//简单使用if (button.tag == kTagStart + 1) { // 销量
        synthesizePopView.hidden = NO ;
        synthesizePopView.dataArray = dataArray[0] ;
        [synthesizePopView.tableView reloadData] ;
        synthesizePopView.CellText = button.titleLabel.text ;
        __weak __typeof__(self) weakSelf = self;
        synthesizePopView.block = ^(NSString * _Nonnull cellTitle) {            
        //如果在 Block 内需要多次 访问 self,则需要使用 strongSelf
            //__strong 确保在 Block 内,strongSelf 不会被释放。
            __strong __typeof(self) strongSelf = weakSelf;
            [strongSelf->priceMenuBtn setTitle:@"价格排序" forState:UIControlStateNormal] ;

            

[button setTitle:cellTitle forState:UIControlStateNormal]

;                         if ([cellTitle isEqualToString:@”由高到低”]) {                 sortTypeStr = 3 ;             }else if ([cellTitle isEqualToString:@”由低到高”]){                 sortTypeStr = 2 ;             }else{                 sortTypeStr = 1 ;             }             [strongSelf initRecommendViewApi] ;         } ;     }

这样一个筛选下拉就做完了。感觉还有很多不足的地方,希望大家指正。

转载时请注明出处及相应链接,本文永久地址:https://blog.yayuanzi.com/25347.html

iOS – tableView类型的筛选框实现

iOS – tableView类型的筛选框实现 微信打赏

iOS – tableView类型的筛选框实现 支付宝打赏

感谢您对作者Miya的打赏,我们会更加努力!    如果您想成为作者,请点我


以上所述就是小编给大家介绍的《iOS – tableView类型的筛选框实现》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Coming of Age in Second Life

Coming of Age in Second Life

Tom Boellstorff / Princeton University Press / 2008-04-21 / USD 29.95

The gap between the virtual and the physical, and its effect on the ideas of personhood and relationships, is the most interesting aspect of Boellstorff's analysis... Boellstorff's portrayal of a virt......一起来看看 《Coming of Age in Second Life》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换