美文网首页iOS 开发 SegmentControl
iOS开发之SegmentedControl显示不同类型 Vie

iOS开发之SegmentedControl显示不同类型 Vie

作者: Sudoke | 来源:发表于2016-04-18 12:08 被阅读3625次

项目中有个需求在同一个页面里需要显示tabbleview和collectionview,这就要用到了 SegmentedControl的分页显示了。

先看效果图:

tableview collection

要用好SegmentedControl,需要先了解他的特性及其使用方法。

一、特性:

1、通常是在单个视图中使用,实现视图中不同 View快速切换。

2、通常位于navbar上或者整个屏幕的上部,当然也可以在屏幕的其他地方,应用较少。

3、一般2到4个分割,超过5个的话每个分割的大小对于用户触碰的体验会很差,且任意时刻,只有一个分割是激活状态的。

二、使用方法:

这个附在代码中介绍。

1、先在SudoVController.m文件的类扩展里加上tableview和collectionview的Delegate和DataSource(SudoVController继承自UIViewController)。

@interface SudoVController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource>

2、控件segment、tableView、collectionView

/** 控件属性*/

@property (nonatomic,weak)UISegmentedControl *segment;//类别

@property(nonatomic, weak) UITableView *tableView;//国内view

@property (nonatomic,weak)UICollectionView *collectionView;//国外view

3、简书写作引用功能引用代码时太不好用了,算了,还是直接的

//创建Segment及tableview和collectionview

-(void)initView{

self.edgesForExtendedLayout = UIRectEdgeNone;//使视图顶部不被navbar覆盖

self.view.backgroundColor = [UIColor whiteColor];//设置view的背景颜色

UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:@[@"国内",@"国外"]];

segment.selectedSegmentIndex = 0;//默认第一个segment被选中

segment.tintColor =Theme_Color;//设置segment背景颜色

segment.frame = CGRectMake(KSCREENW/2-100, 5, 200, 33);//设置segment

[segment addTarget:self action:@selector(changesegment:) forControlEvents:UIControlEventValueChanged];//监听事件,当控件值改变时调用

self.segment = segment;

[self.view addSubview:segment];

/*初始化collectionView*/

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];

UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,40 ,KSCREW, KSCREENH - 64) collectionViewLayout:flowLayout];

[self.view addSubview:collectionView];

collectionView.backgroundColor = [UIColor whiteColor];

//注册

[collectionView registerClass:[HXYHandCollectionCell class] forCellWithReuseIdentifier:reuseIdentifier];

self.collectionView = collectionView;

//设置代理

self.collectionView.delegate = self;

self.collectionView.dataSource = self;

[self.view addSubview:collectionView];

/*初始化tableView*/

UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, self.view.width, self.view.height - 64) style:UITableViewStylePlain];

tableView.backgroundColor = [UIColor whiteColor];

ttableView.delegate = self;

tableView.dataSource = self;

tableView.hidden = YES;

tableView.tableFooterView = [[UIView alloc] init];

self.tableView = tableView;

[self.view addSubview:tableView];

//这里设置初始化view的时候显示tabbleview,

_tableView.hidden = NO;

_collectionView.hidden = YES;

#pragma mark -Action Click

-(void)changesegment:(UISegmentedControl *)segment{

int Index = (int)_segment.selectedSegmentIndex;

switch (Index) {

case 0:

_tableView.hidden = NO;

_collectionView.hidden = YES;

[self tableView];//选中第一个segment

break;

case 1:

_tableView.hidden = YES;

_collectionView.hidden = NO;

[self collectionView];//选中第二个segment

break;

default:

break;

}

}

4、最后分别实现一下tableview和collection view的Delegate及DataSource,这就完成了。

相关文章

网友评论

    本文标题:iOS开发之SegmentedControl显示不同类型 Vie

    本文链接:https://www.haomeiwen.com/subject/soqalttx.html