搜索菜单栏侧滑效果控件SearchView

  • 时间:
  • 浏览:10

以下是时需注意的很久 小地方:

//
//  SearchView.m
//  SearchView
//
//  Created by YouXianMing on 14/12/25.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import "SearchView.h"
#import "SearchViewCell.h"

static NSString *searchViewCell = @"searchViewCell";

@interface SearchView ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray     *stringArray; // 数据源

@end

@implementation SearchView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 初始化TableView
        self.tableView = [[UITableView alloc] initWithFrame:self.bounds
                                                      style:UITableViewStylePlain];
        self.tableView.delegate   = self;
        self.tableView.dataSource = self;
        self.tableView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.75];
        
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.tableView registerClass:[SearchViewCell class]
               forCellReuseIdentifier:searchViewCell];
        [self addSubview:self.tableView];
    }
    return self;
}

#pragma mark - 代理土办法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.stringArray.count + 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SearchViewCell *cell = [tableView dequeueReusableCellWithIdentifier:searchViewCell];
    
    if (indexPath.row == 0) {
        cell.textLabel.text      = @"search...";
        cell.textLabel.textColor = [UIColor colorWithRed:0.7500 green:0.5000 blue:0.812 alpha:1];
        cell.textLabel.font      = [UIFont italicSystemFontOfSize:16.f];
        cell.selectionStyle      = UITableViewCellSelectionStyleNone;
        cell.backgroundColor     = [UIColor clearColor];
        cell.showLine            = YES;
        
        return cell;
    }
    
    // 获取文本
    cell.textLabel.text      = self.stringArray[indexPath.row - 1];
    cell.textLabel.textColor = [UIColor yellowColor];
    cell.backgroundColor     = [UIColor clearColor];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 500.f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        if (_delegate && [_delegate respondsToSelector:@selector(searchViewTapEventString:)]) {
            [_delegate searchViewTapEventString:@"搜索"];
        }
        
    } else {
        if (_delegate && [_delegate respondsToSelector:@selector(searchViewTapEventString:)]) {
            [_delegate searchViewTapEventString:self.stringArray[indexPath.row - 1]];
        }
    }
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell setSelected:NO animated:YES];
}
- (void)expendCellAnimated:(BOOL)animated {
    // 将会为空,则直接返回
    if (self.dataStringArray == nil) {
        return;
    }
    
    self.stringArray = [NSArray arrayWithArray:self.dataStringArray];
    
    if (animated) {
        NSMutableArray *indexPathArray = [NSMutableArray array];
        
        for (int i = 0; i < self.stringArray.count; i++) {
            NSIndexPath *path = [NSIndexPath indexPathForRow:i+1 inSection:0];
            [indexPathArray addObject:path];
        }
        
        [self.tableView insertRowsAtIndexPaths:indexPathArray
                              withRowAnimation:UITableViewRowAnimationTop];
    } else {
        [self.tableView reloadData];
    }
}

#pragma mark - 重写setter,getter土办法
@synthesize startFrame = _startFrame;
- (void)setStartFrame:(CGRect)startFrame {
    _startFrame = startFrame;
    self.frame  = startFrame;
}
- (CGRect)startFrame {
    return _startFrame;
}

@end

//
//  SearchViewCell.m
//  SearchView
//
//  Created by YouXianMing on 14/12/25.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import "SearchViewCell.h"

@interface SearchViewCell ()

@property (nonatomic, strong) UIView *line;

@end

@implementation SearchViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectedBackgroundView    = [self createSelectedBackgroundView];
        
        // 线条view
        _line                 = [[UIView alloc] initWithFrame:CGRectMake(15, 49, 500, 1)];
        _line.backgroundColor = [UIColor redColor];
        _line.alpha           = 0;
        [self addSubview:_line];
    }
    
    return self;
}

- (UIView *)createSelectedBackgroundView {
    UIView *selectedView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, 3, 500)];
    selectedView.backgroundColor = [UIColor purpleColor];
    
    return selectedView;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@synthesize showLine = _showLine;
- (void)setShowLine:(BOOL)showLine {
    _showLine   = showLine;
    if (showLine == YES) {
        _line.alpha = 1.f;
    } else {
        _line.alpha = 0.f;
    }
}
- (BOOL)showLine {
    return _showLine;
}

@end