代码如下:
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];
}
}
}
.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
本文介绍了一个自定义的iOS侧滑菜单视图组件LeftSelectScroll的实现过程,包括如何通过UIButton创建左侧选择菜单,并实现菜单项点击事件及与UITableView联动的功能。
629

被折叠的 条评论
为什么被折叠?



