标签
好久没更新的博客,更新一下吧!写一下标签的实现吧。
AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
ViewController *v = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] init];
[nav addChildViewController:v];
self.window.rootViewController = nav;
return YES;
}
viewController.m中:
#import "ViewController.h"
#import "QZTagsViewController.h"
#define cellidentifier @"cellIdentifier"
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
@interface ViewController ()
<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@end
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
self.navigationItem.title = @"标签列表";
[self.view addSubview:self.tableView];
}
- (NSMutableArray *)dataArr
{
if (!_dataArr) {
NSArray *arr = @[@[@"利用collectionView布局tagsView",@"利用label布局tagsView"],@[@"编辑tagsView"]];
_dataArr = [NSMutableArray arrayWithArray:arr];
}
return _dataArr;
}
- (UITableView *)tableView
{
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH) style:UITableViewStyleGrouped];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.showsVerticalScrollIndicator = NO;
_tableView.backgroundColor = [UIColor clearColor];
}
return _tableView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellidentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier];
}
cell.textLabel.text =self.dataArr[indexPath.section][indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (indexPath.row == 0) {
// CollectionTagsVC *v =[[CollectionTagsVC alloc] init];
// v.navTitle = self.dataArr[indexPath.section][indexPath.row];
// [self.navigationController pushViewController:v animated:YES];
} else {
QZTagsViewController *editVC = [[QZTagsViewController alloc] init];
[self.navigationController pushViewController:editVC animated:YES];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
QZTagsViewController.m
#import "QZTagsViewController.h"
#import "QZTagsView.h"
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
@interface QZTagsViewController ()
@end
@implementation QZTagsViewController
- (NSArray *)tagsArr
{
if (!_tagsArr) {
_tagsArr = @[@"nnn",@"ddd",@"ffgg",@"fdsaf",@"daf"];
}
return _tagsArr;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self setUp];
}
- (void)setUp
{
QZTagsView *qzTagsView = [QZTagsView qzTagsViewWithTagsArr:self.tagsArr];
qzTagsView.frame = CGRectMake(0, 0, kScreenW, 300);
[self.view addSubview:qzTagsView];
}
@end
QZTagsView.m这个是重点
#import "QZTagsView.h"
#import "QZCollectionTagsViewCell.h"
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
#define QZTagsCellIdentifier @"QZTagsView"
@interface QZTagsView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionView;
@end
@implementation QZTagsView
+ (instancetype)qzTagsViewWithTagsArr:(NSArray *)tagsArr
{
QZTagsView *qzTagsView = [[QZTagsView alloc] init];
qzTagsView.tagsArr = [[NSArray alloc] initWithArray:tagsArr];
return qzTagsView;
}
- (instancetype)init
{
self = [super init];
if (self) {
[self setUP];
}
return self;
}
- (void)setUP
{
[self addSubview:self.collectionView];
}
- (UICollectionView *)collectionView
{
if (!_collectionView) {
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.minimumLineSpacing = 5;
flowLayout.minimumInteritemSpacing = 5;
_collectionView= [[UICollectionView alloc] initWithFrame:CGRectMake(15, 20, kScreenW-30, kScreenH-40) collectionViewLayout:flowLayout];
_collectionView.delegate = self;
_collectionView.dataSource = self;
_collectionView.backgroundColor = [UIColor clearColor];
//注册
[_collectionView registerClass:[QZCollectionTagsViewCell class] forCellWithReuseIdentifier:QZTagsCellIdentifier];
}
return _collectionView;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.tagsArr.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSString *title = self.tagsArr[indexPath.row];
CGFloat width = [self widthForLabel:title fontSize:16];
return CGSizeMake(width + 10, 22);
}
/**
* 计算文字长度
*/
- (CGFloat )widthForLabel:(NSString *)text fontSize:(CGFloat)font
{
CGSize size = [text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:font], NSFontAttributeName, nil]];
return size.width;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
QZCollectionTagsViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:QZTagsCellIdentifier forIndexPath:indexPath];
cell.title = self.tagsArr[indexPath.row];
return cell;
}
@end
QZCollectionTagViewCell.h
#import <UIKit/UIKit.h>
@interface QZCollectionTagsViewCell : UICollectionViewCell
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) NSString *title;
@end
.m中
#import "QZCollectionTagsViewCell.h"
@implementation QZCollectionTagsViewCell
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setupUI];
}
return self;
}
- (void)setupUI
{
UILabel *title = [[UILabel alloc] init];
title.font = [UIFont systemFontOfSize:16];
title.layer.cornerRadius = 2.0;
title.layer.masksToBounds = YES;
title.layer.borderWidth = 1.0;
title.textAlignment = NSTextAlignmentCenter;
self.titleLabel = title;
[self.contentView addSubview:title];
}
- (void)setTitle:(NSString *)title
{
_title = title;
self.titleLabel.text = title;
self.titleLabel.frame = CGRectMake(0, 10, MAXFLOAT, 22);
[self.titleLabel sizeToFit];
}
@end
各个类已全部完成调用QZTagsViewController就可完成!ok至此!!!