QQ下拉列表,最近找了一下网上没有类似的例子,今天做了一个Demo
图片可以在评论里留言留下邮箱,
#import "ViewController.h"
@interface
ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)NSMutableArray * dataArray;
@property (nonatomic,weak)UITableView * tabelView;
@property (nonatomic,strong)NSMutableDictionary * imageDict;// 分组对应下拉箭头图片字典
@property (nonatomic,strong)NSMutableDictionary * stateDict;// 分组对应状态字典
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// 加载数据
[self
_loadData];
//
加载视图
[self
_loadView];
}
#pragma mark -_loadData
- (void)_loadData{
NSString * path = [[NSBundle
mainBundle]pathForResource:@"friends.plist"
ofType:nil];
self.dataArray = [NSMutableArray
arrayWithContentsOfFile:path];
}
/**
* 懒加载
*
* @return <#return value description#>
*/
- (NSMutableDictionary *)imageDict{
if (_imageDict ==
nil) {
_imageDict = [NSMutableDictionary
dictionary];
}
return
_imageDict;
}
- (NSMutableDictionary *)stateDict{
if (_stateDict ==
nil) {
_stateDict = [NSMutableDictionary
dictionary];
}
return
_stateDict;
}
#pragma mark -_loadView
- (void)_loadView{
UITableView * tableView = [[UITableView
alloc]initWithFrame:self.view.frame
style:UITableViewStyleGrouped];
tableView.delegate =
self;
tableView.dataSource =
self;
//设置分组头部和尾部的高度
tableView.sectionFooterHeight =
2;
tableView.sectionHeaderHeight =
40;
tableView.rowHeight =
60;
self.tabelView = tableView;
[self.view
addSubview:tableView];
}
#pragma mark - UITableViewDataSource
#pragma mark - 分组头部高度//
不加第一组头显示不出来
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 40;
}
#pragma mark - 返回分组数量
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return
self.dataArray.count;
}
#pragma mark - 设置分组标题
//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
// id temp = self.dataArray[section];
//
// return temp[@"group"];
//}
#pragma mark - 自定义头部视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
static NSString * identy =
@"headView";
// 类似cell的重利用机制
UITableViewHeaderFooterView * hfVeiw = [tableView
dequeueReusableHeaderFooterViewWithIdentifier:identy];
if (!hfVeiw) {
hfVeiw = [[UITableViewHeaderFooterView
alloc]initWithReuseIdentifier:identy];
hfVeiw.contentView.backgroundColor = [UIColor
grayColor];
// 初始化button
UIButton * button = [UIButton
buttonWithType:UIButtonTypeCustom];
button.frame =
CGRectMake(0,
0, self.view.frame.size.width,
40);
[hfVeiw.contentView
addSubview:button];
// 初始化imageView
UIImageView * imageView = [[UIImageView
alloc]initWithFrame:CGRectMake(5,
12, 20,
20)];
UIImage * image = [UIImage
imageNamed:@"disclosure.png"];
imageView.image = image;
imageView.tag =
1001;
[button
addSubview:imageView];
// button添加点击事件
[button addTarget:self
action:@selector(clickAction:)
forControlEvents:UIControlEventTouchUpInside];
}
id temp = self.dataArray[section];
//
拿出button重新设置标题
UIButton * button = [hfVeiw.contentView.subviews
firstObject];
[button setTitle:temp[@"group"]
forState:UIControlStateNormal];
UIImageView * imageView = (UIImageView *)[button
viewWithTag:1001];
//
通过tag值获取点击的是哪个组
button.tag = section;//控件默认的tag都为0,获取未设置tag的控件,会崩,但是设置某控件tag为0则不会
//
将最新的ImageView加入到字典
[self.imageDict
setObject:imageView
forKey:@(section)];
UIImageView * targetImageView = (UIImageView *)self.imageDict[@(section)];
// 为何出现此BUG,如何防止:1.下拉的时候出现重复利用,所以在重复利用后获取从池子取出的imageView,根据当前状态(打开、关闭)设置图片状态,2.点击section展开或者收缩列表,也要从字典获取imageView,并根据当前的状态获取
// 此处解决imageView重复利用的BUG问题 给从池子里取出的imageView根据状态,对其状态条件取反(即当前关闭取反石达开,因为点击之后状态要改变)重新设置图片形状,然后添加动画效果才有动画效果(如果不重新设置图片状态没有动画效果)
if (self.stateDict[@(section)]) {// 如果section组是打开的让从池子随即取出的图片顺时针旋转(图像本身)90°
targetImageView.transform =
CGAffineTransformMakeRotation(M_PI_2);
}else{
targetImageView.transform =
CGAffineTransformIdentity;// 如果section组是关闭的是使图片置为原始图片状态
}
return hfVeiw;
}
#pragma mark - clickAction:
- (void)clickAction:(UIButton *)button{
NSInteger section = button.tag;
BOOL isOpen = (self.stateDict[@(section)] ==
nil);//isOpen default is NO
if (isOpen) {
[self.stateDict
setObject:@(1)
forKey:@(section)];
}else{
[self.stateDict
removeObjectForKey:@(section)];
}
NSLog(@"----------%li,%@,%i",section,self.stateDict[@(section)],isOpen);
NSIndexSet * set = [NSIndexSet
indexSetWithIndex:section];
[self.tabelView
reloadSections:set withRowAnimation:UITableViewRowAnimationFade];//
//
设置动画
//
通过字典获取对应的imageView
UIImageView * imageView =
self.imageDict[@(section)];
if (!isOpen) {// 如果下拉列表是关闭的,就打开它(图片顺时针旋转90)
imageView.transform =
CGAffineTransformMakeRotation(M_PI_2);
}else{// 如果下拉列表是打开的,就关闭它(图片置为原始状态)
imageView.transform =
CGAffineTransformIdentity;//
恢复到原始状态
}// 以上if解决图片没有动画效果
[UIView
animateWithDuration:0.3
animations:^{
//
重新判断状态
if (isOpen) {
imageView.transform =
CGAffineTransformMakeRotation(M_PI_2);
}else{
imageView.transform =
CGAffineTransformIdentity;
}
}];
}
#pragma mark - 返回每组的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
id temp = self.dataArray[section];
NSArray * array = [temp
objectForKey:@"friends"];
if (self.stateDict[@(section)]!=nil) {
return array.count;
}
NSLog(@"************%@",self.stateDict[@(section)]);
return 0;
}
#pragma mark - cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath{
static NSString * identy =
@"myTable";
UITableViewCell * cell = [tableView
dequeueReusableCellWithIdentifier:identy];
if (cell == nil) {
cell = [[UITableViewCell
alloc]initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:identy];//UITableViewCellStyleSubtitle(选择有副标题的,以添加副标题)
}
//
增加标题
id temp = self.dataArray[indexPath.section];
NSArray * section = [temp
objectForKey:@"friends"];
cell.textLabel.text = section[indexPath.row];
//
增加头像
NSString * imageName = [NSString
stringWithFormat:@"head%d",arc4random_uniform(7)+1];
cell.imageView.image = [UIImage
imageNamed:imageName];
//
增加子标题
cell.detailTextLabel.text =
@"在线";
return cell;
}
@end
ios QQ下拉列表 UITableViewHeaderFooterViewios QQ下拉列表 UITableViewHeaderFooterViewios
QQ下拉列表 UITableViewHeaderFooterViewios QQ下拉列表 UITableViewHeaderFooterViewios
QQ下拉列表 UITableViewHeaderFooterViewios QQ下拉列表 UITableViewHeaderFooterViewios QQ下拉列表 UITableViewHeaderFooterViewios
QQ下拉列表 UITableViewHeaderFooterViewios QQ下拉列表 UITableViewHeaderFooterViewios QQ下拉列表 UITableViewHeaderFooterViewios
QQ下拉列表 UITableViewHeaderFooterViewios QQ下拉列表 UITableViewHeaderFooterViewios
QQ下拉列表 UITableViewHeaderFooterViewios QQ下拉列表 UITableViewHeaderFooterViewios QQ下拉列表 UITableViewHeaderFooterViewios
QQ下拉列表 UITableViewHeaderFooterViewios QQ下拉列表 UITableViewHeaderFooterViewios QQ下拉列表 UITableViewHeaderFooterViewios
QQ下拉列表 UITableViewHeaderFooterViewios QQ下拉列表 UITableViewHeaderFooterViewios
QQ下拉列表 UITableViewHeaderFooterViewios QQ下拉列表 UITableViewHeaderFooterViewios QQ下拉列表 UITableViewHeaderFooterViewios
QQ下拉列表 UITableViewHeaderFooterView