自定义弹出选择列表

基于tableview封装的弹出选择列表,
效果图:


DFC901A8-E8C9-4F91-8612-D3D12B7BB806.png

使用起来非常方便。

首先定义自己的代理方法

@protocol PopListViewDataSource <NSObject>

- (NSInteger)PopListView:(PopListView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)PopListView:(PopListView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
@end

@protocol PopListViewDelegate <NSObject>

- (void)PopListView:(PopListView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)PopListView:(PopListView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath;

@end```
并实现

pragma mark UITableViewDataSource

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.dataSource && [self.dataSource respondsToSelector:@selector(PopListView:numberOfRowsInSection:)]) {
    return [self.dataSource PopListView:self numberOfRowsInSection:section];
    }
    return 0;
    }
  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.dataSource && [self.dataSource respondsToSelector:@selector(PopListView:cellForRowAtIndexPath:)]) {
    return [self.dataSource PopListView:self cellForRowAtIndexPath:indexPath];
    }
    return nil;
    }

pragma mark UITableViewDelegate

  • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.delegate && [self.delegate respondsToSelector:@selector(PopListView:didSelectRowAtIndexPath:)]) {
    [self.delegate PopListView:self didSelectRowAtIndexPath:indexPath];
    }
    }
  • (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.delegate && [self.delegate respondsToSelector:@selector(PopListView:didDeselectRowAtIndexPath:)]) {
    [self.delegate PopListView:self didDeselectRowAtIndexPath:indexPath];
    }
    }```
    再加上一些显示和隐藏的方法即可

具体使用如下

- (void)viewDidLoad {
    [super viewDidLoad];
    ListView = [[PopListView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
    ListView.delegate = self;
    ListView.dataSource = self;
    ListView.TitleName.text = @"选项列表";
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 100, 200, 40);
    [button setTitle:@"点击" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(showListView) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}
- (void)showListView{
    array = @[@"1",@"2",@"3",@"4",@"5"];
    [ListView.MainPopListView reloadData];
    [ListView show];
}
- (NSInteger)PopListView:(PopListView *)tableView numberOfRowsInSection:(NSInteger)section{
    return array.count;
}
- (UITableViewCell *)PopListView:(PopListView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * identifier = @"identifier";
    UITableViewCell * cell = [tableView dequeueResuablePopCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%@",array[indexPath.row]];
    
    return cell;
}
- (void)PopListView:(PopListView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%@",array[indexPath.row]);
    [self performSelectorOnMainThread:@selector(listDismiss) withObject:nil waitUntilDone:NO];
}
- (void)listDismiss{
    [ListView dismiss];
}```

代码在github上:[demo](https://github.com/shenlulu1016/PopListViewDemo).

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 181,139评论 25赞 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 17,198评论 4赞 61
  • 相信我,这儿有你需要的工具。 作为深度互联网工具挖掘者,会遇到各种好玩有趣的网站,我会持续把自己发现的非常有趣或者...
    ruruii如蕤阅读 344评论 0赞 0
  • 人生短暂,生命无常。 今天感受到千里之外一个生命的垂死挣扎。当她万念俱灰时,选择以死亡寻求解脱,好在最终安好。 生...
    虚线阅读 483评论 0赞 0
  • 方正见状只好重新关好车门回过头冲郑广坤说道 :“大仙儿你看什么呢?” 郑广坤头也不回地答道 :“我早就说过,像政府...
    长白居士阅读 260评论 0赞 0

友情链接更多精彩内容