tableview嵌套scrollview(手势冲突暂无发现)




问题:像这样能进行滑动的界面。如何去搭建

思路:创建分组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;

    }

}


2.在自定义cell中

//

//  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












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS 开发中,嵌套在 `UIScrollView` 中的 `UITableView` 在滑动时可能会与 `UIScrollView` 的滑动手势产生冲突,导致无法正常滑动。这个问题可以通过以下两种方式解决: 1. 禁用 `UIScrollView` 的滑动手势 可以通过设置 `UIScrollView` 的 `panGestureRecognizer` 的 `enabled` 属性为 `NO` 来禁用滑动手势,这样就不会与 `UITableView` 的滑动手势产生冲突了。 ```objc scrollView.panGestureRecognizer.enabled = NO; ``` 2. 实现 `UIGestureRecognizerDelegate` 协议的方法 在 `UIViewController` 中实现 `UIGestureRecognizerDelegate` 协议的 `gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:` 方法,可以控制两个手势是否允许同时识别。在这个方法中,可以判断当前的手势是否为 `UIScrollView` 的滑动手势,如果是,则允许与 `UITableView` 的滑动手势同时识别,否则不允许。 ```objc - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { if ([gestureRecognizer.view isKindOfClass:[UIScrollView class]] && [otherGestureRecognizer.view isKindOfClass:[UITableView class]]) { return YES; } return NO; } ``` 需要注意的是,在实现这个方法时,要将 `UIScrollView` 的 `delegate` 设置为当前的 `UIViewController`,否则这个方法不会被调用。 ```objc scrollView.delegate = self; ``` 以上两种方式都可以解决嵌套在 `UIScrollView` 中的 `UITableView` 滑动手势冲突的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值