自定义弹出选择列表

2017-05-22  本文已影响21人  心底碎片

基于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

pragma mark UITableViewDelegate

具体使用如下

- (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).

上一篇下一篇

猜你喜欢

热点阅读