问题:像这样能进行滑动的界面。如何去搭建
思路:创建分组tableview。。每组里面嵌套一个scrollview.
1.在controller中
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
[self initData];
[self.view addSubview:self.tableView];
//在ios7及其以上,需添加这个属性为no
self.automaticallyAdjustsScrollViewInsets = NO;
}
- (void)initData
{
//自己创建模型
self.modelArray = [NSMutableArray array];
NSMutableArray *mutableArray1 = [NSMutableArray array];
NSArray *array1 = [NSArray arrayWithObjects:@"订单",@"会议室", @"任务", @"预订", @"交租", @"装修", @"维修", @"保洁", nil];
for (int i = 0; i<array1.count; i++)
{
NSMutableDictionary *infoDict = [[NSMutableDictionary alloc]init];
[infoDict setValue:array1[i]forKey:@"title"];
NSString *str = [NSString stringWithFormat:@"%d",i];
[infoDict setValue:str forKey:@"number"];
MeModel *model = [[MeModel alloc]initWithDict:infoDict];
[mutableArray1 addObject:model];
}
NSMutableArray *mutableArray2 = [NSMutableArray array];
NSArray *array2 = [NSArray arrayWithObjects:@"钱包",@"优惠券", @"余额", @"银行卡", nil];
for (int i = 0; i<array2.count; i++)
{
NSMutableDictionary *infoDict = [[NSMutableDictionary alloc]init];
[infoDict setValue:array2[i]forKey:@"title"];
NSString *str = [NSString stringWithFormat:@"%d",i];
[infoDict setValue:str forKey:@"number"];
MeModel *model = [[MeModel alloc]initWithDict:infoDict];
[mutableArray2 addObject:model];
}
NSMutableArray *mutableArray3 = [NSMutableArray array];
NSArray *array3 = [NSArray arrayWithObjects:@"提醒", @"会议室", @"任务", @"预订", @"交租", @"装修", @"维修", @"保洁", nil];
for (int i = 0; i<array3.count; i++)
{
NSMutableDictionary *infoDict = [[NSMutableDictionary alloc]init];
[infoDict setValue:array3[i]forKey:@"title"];
NSString *str = [NSString stringWithFormat:@"%d",i];
[infoDict setValue:str forKey:@"number"];
MeModel *model = [[MeModel alloc]initWithDict:infoDict];
[mutableArray3 addObject:model];
}
NSMutableArray *mutableArray4 = [NSMutableArray array];
NSArray *array4 = [NSArray arrayWithObjects:@"发布", @"装修", @"维修", @"保洁", @"房务", @"任务", @"活动", nil];
for (int i = 0; i<array4.count; i++)
{
NSMutableDictionary *infoDict = [[NSMutableDictionary alloc]init];
[infoDict setValue:array4[i]forKey:@"title"];
NSString *str = [NSString stringWithFormat:@"%d",i];
[infoDict setValue:str forKey:@"number"];
MeModel *model = [[MeModel alloc]initWithDict:infoDict];
[mutableArray4 addObject:model];
}
[self.modelArray addObject:mutableArray1];
[self.modelArray addObject:mutableArray2];
[self.modelArray addObject:mutableArray3];
[self.modelArray addObject:mutableArray4];
}
-(UITableView *)tableView
{
if (_tableView == nil) {
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 184, self.view.frame.size.width, self.view.frame.size.height-184) style:UITableViewStyleGrouped];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.showsHorizontalScrollIndicator = NO;
_tableView.showsVerticalScrollIndicator = NO;
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.bounces = NO;
}
return _tableView;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = @"cellId";
MeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (!cell) {
cell = [[MeTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
}
cell.selectionStyle = UITableViewCellSeparatorStyleNone;
cell.medelegate = self;
NSArray *tempArray =self.modelArray[indexPath.section];
[cell loadNSArray:tempArray];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60;
}
//分组的时候。注意。这个不要return 0 。。那是没用的
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section ==0) {
return 1.0f;
}
else{
return 5;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
if (section ==0) {
return 5;
}
else{
return 5;
}
}
//
// MeTableViewCell.m
// uicollectionvie横向滑动
//
// Created by 洪福清 on 16/11/11.
// Copyright © 2016年 BJTYL. All rights reserved.
//
#define ItemWidth [UIScreen mainScreen].bounds.size.width / 4
#import "MeTableViewCell.h"
#import "MeModel.h"
@interface MeTableViewCell ()<UIScrollViewDelegate>
@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) UILabel *themeLabel;
@property (strong,nonatomic) UILabel *countLabel;
@end
@implementation MeTableViewCell
static NSInteger countTag = 0;
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self setup];
}
return self;
}
- (void)setup
{
[self addSubview:self.scrollView];
[self addSubview:self.themeLabel];
}
-(void)loadNSArray:(NSArray *)array
{
MeModel *model = array[0];
self.themeLabel.text = [NSString stringWithFormat:@"%@",model.title];
self.scrollView.contentSize = CGSizeMake(ItemWidth * (array.count - 1), 60);
for (int i = 1; i < array.count; i ++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(ItemWidth * (i - 1), 0, ItemWidth, 60);
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
countTag = countTag +1;
button.tag = countTag;
[self.scrollView addSubview:button];
MeModel *model = array[i];
[button setTitle:model.title forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
}
}
- (void)buttonClick:(UIButton *)btn
{
NSString *str = [NSString stringWithFormat:@"%lu",btn.tag];
if ([self.medelegate respondsToSelector:@selector(passnumberofitem:)]) {
[self.medelegate performSelector:@selector(passnumberofitem:) withObject:str];
}
}
-(UIScrollView *)scrollView
{
if (_scrollView == nil) {
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(ItemWidth, 0, [UIScreen mainScreen].bounds.size.width-ItemWidth,60)];
_scrollView.delegate = self;
_scrollView.backgroundColor = [UIColor whiteColor];
_scrollView.showsVerticalScrollIndicator = NO;
_scrollView.showsHorizontalScrollIndicator = NO;
}
return _scrollView;
}
-(UILabel *)themeLabel
{
if (_themeLabel == nil) {
_themeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ItemWidth, 60)];
_themeLabel.textColor = [UIColor blackColor];
_themeLabel.textAlignment = NSTextAlignmentCenter;
}
return _themeLabel;
}
-(UILabel *)countLabel
{
if (_countLabel == nil) {
_countLabel =[[UILabel alloc] init];
_countLabel.textColor = [UIColor blackColor];
}
return _countLabel;
}
@end