tableview实现qq好友列表点击拉伸的效果
2015-04-05 09:19
651 查看
uitableview大概是objective-c开发中最重要的控件了,几乎市面上能够下载的应用中都有使用到它。qq的好友列表也是通过tableview实现的,具体实现大概是在将每一个sectionHeaderView设置为列表名称,然后点击之后加载数据源拉伸,今天就仿照qq的好友列表做一个出来,效果图如下
上面的效果是怎么实现的呢?每一行列表是一个section,我们用tableview的数据源方法返回button作为每一个section的headerView视图,然后在button上面增加列表名字的控件,点击button时就刷新tableview对应的section数据,创建button的代码:
Group.h
点击section后触发事件
上面这几步核心步骤完成了,其余的步骤按照常规的tableview数据源方法实现就完成了
上面的效果是怎么实现的呢?每一行列表是一个section,我们用tableview的数据源方法返回button作为每一个section的headerView视图,然后在button上面增加列表名字的控件,点击button时就刷新tableview对应的section数据,创建button的代码:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { //组的信息 Group *group = groups[section]; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(0, 0, self.view.frame.size.width, 40); button.backgroundColor = [UIColor colorWithRed:235.0/255.0 green:235.0/255.0 blue:235.0/255.0 alpha:1.0]; [button addTarget:self action:@selector(tapHeadView:) forControlEvents:UIControlEventTouchUpInside]; button.tag = section + 1; //箭头 UIImageView *arrowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, button.frame.size.height/2 - 10, 20, 20)]; [button addSubview:arrowImageView]; if (group.isOpen) { arrowImageView.image = [UIImage imageNamed:@"buddy_header_arrow_down"]; } else { arrowImageView.image = [UIImage imageNamed:@"buddy_header_arrow_right"]; } //组的名字 UILabel *groupNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(arrowImageView.frame), 0, 200, button.frame.size.height)]; groupNameLabel.text = group.groupName; [button addSubview:groupNameLabel]; //在线人数 UILabel *onlineLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 70, 0, 60, button.frame.size.height)]; onlineLabel.text = [NSString stringWithFormat:@"%d/%d",group.onlineNumber,group.friends.count]; onlineLabel.textAlignment = NSTextAlignmentRight; [button addSubview:onlineLabel]; //线 UIImageView *lineImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, button.frame.size.height - 2, button.frame.size.width, 2)]; lineImage.image = [UIImage imageNamed:@"line"]; [button addSubview:lineImage]; return button; }
Group.h
@interface Group : NSObject /** 组的名字 */ @property (nonatomic,copy) NSString *groupName; /** 在线人数 */ @property (nonatomic,assign) NSInteger onlineNumber; /** 好友 */ @property (nonatomic,strong) NSMutableArray *friends; /** 打开或者闭合状态 */ @property (nonatomic,assign) BOOL isOpen; @end
点击section后触发事件
- (void)tapHeadView:(UIButton *)button { //取组的信息 Group *group = groups[button.tag - 1]; //打开 if (group.isOpen) { group.isOpen = NO; } else //闭合 { group.isOpen = YES; } //刷新当前分区 [friendTableView reloadSections:[NSIndexSet indexSetWithIndex:button.tag - 1] withRowAnimation:UITableViewRowAnimationFade]; }
上面这几步核心步骤完成了,其余的步骤按照常规的tableview数据源方法实现就完成了
相关文章推荐
- 可拖动拉伸的左右分栏效果,本例子是用Table以及JS来实现的,左右分栏可改变,拖动中间的边框就可实现宽度的调整,点击时可设置为隐藏左侧列表。
- 可拖动拉伸的左右分栏效果,本例子是用Table以及JS来实现的,左右分栏可改变,拖动中间的边框就可实现宽度的调整,点击时可设置为隐藏左侧列表。
- swift-tableView 代理方法实现点击按钮删除效果
- iOS tableview实现顶部拉伸效果
- 实现类似QQ聊天列表的segment点击切换tableView的数据功能
- IOS_实现tableview向上滚动全屏效果与点击展开动画
- iOS TableView实现QQ好友列表(二)
- iOS TableView实现QQ好友列表(二)
- 使用RecyclerView 简单实现QQ好友列表展开效果
- iOS tableView实现顶部图片拉伸效果
- tableView中实现headView图片拉伸效果的三种解决方案
- 【利用TableView实现QQ好友列表】
- iOS TableView实现QQ好友列表
- iOS TableView实现QQ好友列表(三)
- iOS TableView实现QQ好友列表(三)
- iOS TableView实现QQ好友列表(一)
- iOS TableView实现QQ好友列表(一)
- IOS详解TableView —— QQ好友列表的实现
- 8-20学习练习[用两个tableview实现类似省市联动选择效果]
- 8-20学习练习[用两个tableview实现类似省市联动选择效果]