IOS仿微信朋友圈好友展示

简介:

前几天小伙伴要帮他做一个群聊功能,里面有好友列表,要求和微信的差不多(见下图),让小伙伴自己实现了下,他将CollectionView放在tableView的tableHead中,可是当添加好友或删除好友刷新数据源的时候并没有效果。让他将CollectionView放在tableView的cell中,结果是数据刷新了可是还是有问题删除后刷新数据时CollectionView的高度变的有问题,我就调了下,实现比较简单,只是一些细节问题,现在效果还蛮不错的,分享一下.

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.效果图

 


相关文章
|
6月前
|
API 数据安全/隐私保护 开发者
企业微信自动加好友软件,导入手机号批量添加微信好友,python版本源码分享
代码展示了企业微信官方API的合规使用方式,包括获取access_token、查询部门列表和创建用户等功能
|
iOS开发 开发者
iOS微信分享配置universal links步骤
iOS微信分享配置universal links步骤
5506 58
|
6月前
|
数据安全/隐私保护 开发者 安全
微信加5000好友软件有吗?是不是真的
作为一名长期关注社交平台技术发展的开发者,最近经常被问到"微信加5000好友软件"是否真实存在的问题
|
7月前
|
Java API
wxid添加微信好友工具,免费微信wxid转换器二维码,jar实现仅供学习参考
本项目实现微信ID与wxid的转换及二维码生成功能,核心逻辑基于ZXing库完成QR编码,支持文件批量导入导出。
|
7月前
|
编解码 Java
wxid加微信好友工具,二维码转换工具,微信号转wxid插件【仅供学习参考】
本工具基于JAVA实现微信ID转换功能,支持wxid、微信号与二维码之间的相互转换。开发中使用ZXing库(版本3.5.1)完成二维码编解码,并设计核心类`WxidConverter`实现关键转换逻辑。
|
6月前
|
移动开发 PHP 数据安全/隐私保护
抖音一键跳转微信加好友
抖音跳转微信加好友的技术实现方案 1. 技术背景与需求分析
|
6月前
|
移动开发 安全 数据安全/隐私保护
怎么通过链接跳转加微信好友
深度链接技术在微信好友添加中的应用 技术背景
|
7月前
|
Android开发
微信加好友脚本自动,手机号自动添加定时,可群聊通讯录添加
这是一款基于 AutoJS 的微信自动化脚本,适用于 Android 7.0+ 系统,实现定时批量添加好友功能。采用分层设计(主控模块+功能模块+配置模块)

热门文章

最新文章