滑动视图和表示图的综合应用

本文介绍了一个自定义的iOS侧滑菜单视图组件LeftSelectScroll的实现过程,包括如何通过UIButton创建左侧选择菜单,并实现菜单项点击事件及与UITableView联动的功能。

代码如下:

1、新建一个scrollView视图,命名为LeftSelectScroll ,代码如下

.h 文件

#import <UIKit/UIKit.h>

//定义协议

@protocol LeftSelectScrollDelegate <NSObject>

- (void) clickLeftSelectScrollButton :(NSInteger)indexPath;

@end


@interface LeftSelectScroll : UIScrollView

//定义属性

@property (nonatomic,strong) NSArray *leftSeclectArray;


@property (nonatomic,strong) id <LeftSelectScrollDelegate> leftSelectDelegate;


//定义方法

- (void)setLeftSeclectArray:(NSArray *)leftSeclectArray;


- (void) setSelectButtonWithIndexPathSection:(NSInteger)indexPathSection;



@end

.m 文件中的主要方法

#pragma mark - 创建左侧视图的内容

- (void) setLeftSeclectArray:(NSArray *)leftSeclectArray {

    _leftSeclectArray = leftSeclectArray;

    //利用for循环来创建

    for (int i = 0; i < _leftSeclectArray.count; i++) {

        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 53* i, kWidth*0.25, 53)];

        [button setTitle:_leftSeclectArray[i] forState:UIControlStateNormal];

        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];

        button.backgroundColor = [UIColor purpleColor];

        [button setBackgroundColor:[UIColor whiteColor]];

        

        UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, button.frame.size.height - 0.5, button.frame.size.width, 0.5)];

        lable.backgroundColor  =[UIColor grayColor];

        [button addSubview:lable];

        [self addSubview:button];

        button.tag = 1500+ i;

        [button addTarget:self action:@selector(clickLeftSelectButton:) forControlEvents:UIControlEventTouchUpInside];

        if ( i == 0) {

            [button setSelected:YES];

            [button setBackgroundColor:[UIColor orangeColor]];

            tempSelectButton = button;

        }

    }

}

#pragma mark - 按钮的实现方法

- (void)clickLeftSelectButton:(UIButton *) button {

    [tempSelectButton setSelected:NO];

    [tempSelectButton setBackgroundColor:[UIColor whiteColor]];

    

    [button setBackgroundColor:[UIColor redColor]];

    [button setSelected:YES];

    tempSelectButton = button;

    

    NSInteger tag = button.tag - 1500;

    if (self.leftSelectDelegate && [self.leftSelectDelegate respondsToSelector:@selector(clickLeftSelectScrollButton:)]) {

        [self.leftSelectDelegate clickLeftSelectScrollButton:tag];

    }

}

#pragma mark - 实现点击按钮就要跳转到的表示图的分组的索引值

- (void) setSelectButtonWithIndexPathSection:(NSInteger)indexPathSection {

    for (int i = 0; i < _leftSeclectArray.count; i ++) {

        NSInteger tag = i+1000;

        UIButton *btn = (UIButton *)[self viewWithTag:tag];

        if (btn.tag == indexPathSection + 1000) {

            tempSelectButton = btn;

            [btn setSelected:YES];

            btn.backgroundColor = [UIColor orangeColor];

        } else {

            [btn setSelected:NO];

            btn.backgroundColor = [UIColor yellowColor];

        }

    }

}


2、新建一个ViewController。命名为:

.m文件中的主要代码为:

(1)实现代理的方法:

//代理方法

- (void) clickLeftSelectScrollButton :(NSInteger)indexPath {

    

    isScrollSetSelect = NO;

    

    [_tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:indexPath] atScrollPosition:UITableViewScrollPositionTop animated:YES];

}

(2)头部视图的方法:

//实际需要会修改

-(UIView*)viewForHeaderView:(NSInteger)parama{

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kWidth, 32)];

    label.backgroundColor = [UIColor grayColor];

    if (leftDataSource.count != 0) {

        label.text = leftDataSource[parama];

        //        [NSString stringWithFormat:@"%ld",(long)parama];

    }

    return label;

   // NSLog(@"%@",label.text);

}


全部代码链接为:

http://note.youdao.com/yws/public/redirect/share?id=845663a5081473a9a00bca61c94b4c4e&type=false


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值