基于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).
网友评论