IOS仿微信朋友圈好友展示
2015-10-20 10:22
471 查看
前几天小伙伴要帮他做一个群聊功能,里面有好友列表,要求和微信的差不多(见下图),让小伙伴自己实现了下,他将CollectionView放在tableView的tableHead中,可是当添加好友或删除好友刷新数据源的时候并没有效果。让他将CollectionView放在tableView的cell中,结果是数据刷新了可是还是有问题删除后刷新数据时CollectionView的高度变的有问题,我就调了下,实现比较简单,只是一些细节问题,现在效果还蛮不错的,分享一下.
1.定义CollectionViewCell,并为删除按钮添加一个block
2.用TableView+CollectionView显示 此时注意:计算CollectionView的高度时,要注意行数是整数而且要有余进1
3.效果图
1.定义CollectionViewCell,并为删除按钮添加一个block
2.用TableView+CollectionView显示 此时注意:计算CollectionView的高度时,要注意行数是整数而且要有余进1
#import "ViewController.h" #import "CustomTableViewCell.h" #import "TKQunSheZhiCollectionViewCell.h" @interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout> { UITableView *_mTableView; CGFloat _imgHeight; UICollectionView *_mCollectionView; NSMutableArray *_mDataArray; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self initTableView]; _mDataArray = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"3",@"4",@"5", nil]; } //----------TableView--- - (void)initTableView { _mTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; _mTableView.delegate = self; _mTableView.dataSource = self; [self.view addSubview:_mTableView]; } //UITableViewDelegate,UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 3; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { static NSString *cellIdentifier = @"UITableViewCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } if (_mCollectionView) { [_mCollectionView removeFromSuperview]; } [cell.contentView addSubview:[self createCollectionView]]; return cell; }else{ static NSString *cellStr = @"customCell"; CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr]; if (cell == nil) { cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCell" owner:nil options:nil] lastObject]; } cell.nameLabel.text = @"AAA"; return cell; } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { float height=((self.view.frame.size.width - 50)/4 + 30) * ((int)(_mDataArray.count + 4)/4) + 20; return height; }else{ return 44; } } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 0.001; } //---------CollectionView---- - (UICollectionView *)createCollectionView { UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; _imgHeight = (self.view.frame.size.width - 50)/4; layout.itemSize = CGSizeMake((self.view.frame.size.width - 50)/4, _imgHeight + 20); layout.minimumLineSpacing = 10; layout.minimumInteritemSpacing = 10; layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10); _mCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, ((_imgHeight + 30) * ((int)(_mDataArray.count + 4)/4)) + 20) collectionViewLayout:layout]; _mCollectionView.delegate = self; _mCollectionView.dataSource = self; _mCollectionView.backgroundColor = [UIColor whiteColor]; _mCollectionView.scrollEnabled = NO; [_mCollectionView registerNib:[UINib nibWithNibName:@"TKQunSheZhiCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"TKQunSheZhiCollectionViewCell"]; return _mCollectionView; } //UICollectionViewDataSource,UICollectionViewDelegate - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return _mDataArray.count + 1; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"%@",indexPath); } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { TKQunSheZhiCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TKQunSheZhiCollectionViewCell" forIndexPath:indexPath]; if (indexPath.row < 1) { cell.mNickName.hidden = YES; cell.mDelBtn.hidden = YES; if (indexPath.row == 0) { cell.mHeadImg.image = [UIImage imageNamed:@"qunzu-jiahao"]; } else if (indexPath.row == 1){ cell.mHeadImg.image = [UIImage imageNamed:@"qunzu-jianhao"]; } }else{ cell.delMemberBlock = ^() { [_mDataArray removeLastObject]; [_mTableView reloadData]; }; } return cell; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
3.效果图
相关文章推荐
- 微信JS SDK Demo
- 公众号的菜单栏
- iOS 支付 [支付宝、银联、微信]
- 微信公众号80端口映射详解(二)
- 限制网页仅可在微信客户端打开
- OD调试篇6--对一些真正的小程序进行一点点的修改
- 013.遍历数组
- nodejs微信开发--设置自定义菜单
- Android/iOS微信6.3.5同时发布更新 支持群视频聊天、群公告
- 总结实现微信支付所遇到的坑
- 【高仿微信系列】微信录制小视频
- iOS 9系统策略更新,微信分享无法使用解决办法
- 微信订阅返回消息
- Android 微信登陆
- 微信公众号接入
- 微信高级群发接口 {"errcode":40008,"errmsg":"invalid message type hint: [aRIDBA0726age9]"}
- 微信分享核心代码
- 关于微信分享、微信登录秘钥问题,及点击分享闪一下,提示分享成功,实际上没有分享成功
- 微信自定义菜单view类型获取openid访问网页
- java50个小程序