「ios開発」ComboBoxをカスタマイズするサンプルプログラム

Objective-Cコード下記:
#import <UIKit/UIKit.h>
#import “CCButtonDelegate.h"
#define ROW_HEIGHT 30
#define ROWLINE 6
#define ROWWIDTH 280

@protocol CCComboBoxDelegate;
@interface CCComboBox : UIView<UITableViewDataSource, UITableViewDelegate, CCButtonDelegate>
{
@private
UIButton *_button;
UITableView *_tableView;
NSInteger _currentRow;
NSUInteger _rowMaxLines;
BOOL isHidden;
UIView *_overlayView;
}
@property (assign, nonatomic) id<CCComboBoxDelegate> delegate;
@property (strong, nonatomic, setter = setDataArray:) NSMutableArray *dataArray;

– (id)initWithFrame:(CGRect)frame;
– (void)setDataArray:(NSMutableArray *)dataArray;
@end

@interface CCComboBox (UIButton)
– (void)setComboBoxBackgroundImage:(UIImage *)image forState:(UIControlState)state;
– (void)setComboBoxFrame:(CGRect)frame;
– (id)getComboBoxSelectedItem;

@end

@interface CCComboBox (UITableView)
– (void)setComboBoxShowMaxLine:(NSUInteger)count;

@end

/*実現コード*/
#import “CCComboBox.h"
#import “UIButton+CCButton.h"
#import “CCComboBoxDelegate.h"

@interface CCComboBox (Private)

– (void)showOrHidden:(BOOL)hidden;
– (void)fadeIn;
– (void)fadeOut;

@end

@implementation CCComboBox

– (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.frame = frame;
_currentRow = 0;
_dataArray = [NSMutableArray arrayWithCapacity:0];
isHidden = YES;
_button = [[UIButton alloc] initWithNormalImage:nil andHighLighted:nil frame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
[_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[_button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
_button.contentEdgeInsets = UIEdgeInsetsMake(5, 10, 5, frame.size.width/18.0);
_button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[_button addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_button];
}
return self;
}

– (void) setDataArray:(NSMutableArray *)dataArray
{
_dataArray = dataArray;
if (_dataArray.count > 0) {
if (_delegate) {
[_delegate selected:self atIndex:0];
}
[_button setTitle:[_dataArray objectAtIndex:0] forState:UIControlStateNormal];
}
if (_tableView) {
[_tableView reloadData];
}

}

#pragma mark – UIButton
– (void)setComboBoxBackgroundImage:(UIImage *)image forState:(UIControlState)state
{
[_button setBackgroundImage:image forState:state];
}

– (void)setComboBoxFrame:(CGRect)frame
{
self.frame = frame;
_button.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
}

– (id)getComboBoxSelectedItem
{
return [_dataArray objectAtIndex:_currentRow];
}

#pragma mark – UITableView
– (void)setComboBoxShowMaxLine:(NSUInteger)count
{
_rowMaxLines = count;
if (_tableView) {
_tableView.frame = CGRectMake(0, 0, ROWWIDTH, _dataArray.count > _rowMaxLines ? ROW_HEIGHT *_rowMaxLines : ROW_HEIGHT * _dataArray.count);
}
}

#pragma mark – UIButtonDelegate
– (void)clickAction:(id)sender
{
if (!_tableView) {
_rowMaxLines = _rowMaxLines == 0 ? ROWLINE : _rowMaxLines;
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ROWWIDTH, _dataArray.count > _rowMaxLines ? ROW_HEIGHT * _rowMaxLines : ROW_HEIGHT * _dataArray.count) style:UITableViewStylePlain];
_tableView.dataSource = self;
_tableView.delegate = self;

_tableView.layer.borderWidth = 1;
_tableView.layer.borderColor = [[UIColor grayColor] CGColor];
_tableView.layer.cornerRadius = 5;

_overlayView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
_overlayView.backgroundColor = [UIColor colorWithRed:.16 green:.17 blue:.21 alpha:.5];
}
[self showOrHidden:(isHidden = !isHidden)];
}

#pragma mark – UITableViewDataDelegate
– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [_dataArray objectAtIndex:[indexPath row]];
cell.textLabel.font = [UIFont fontWithName:@"Arial" size:14];
return cell;
}

– (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _dataArray.count;
}

#pragma mark – UITableViewDelegate
– (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[_button setTitle:[_dataArray objectAtIndex:[indexPath row]] forState:UIControlStateNormal];
if (_delegate && _currentRow != [indexPath row]) {
[_delegate selected:self atIndex:[indexPath row]];
}
_currentRow = [indexPath row];
[self showOrHidden:(isHidden = !isHidden)];
}

– (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return ROW_HEIGHT;
}

– (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath row] % 1 == 0) {
//cell.backgroundColor = [UIColor grayColor];
}
}

#pragma mark – private
– (void)showOrHidden:(BOOL)hidden
{
if (hidden) {
[self fadeOut];
}else {
UIWindow *keywindow = [[UIApplication sharedApplication] keyWindow];
[keywindow addSubview:_overlayView];
[keywindow addSubview:_tableView];
_tableView.center = CGPointMake(keywindow.bounds.size.width/2.0f,
keywindow.bounds.size.height/2.0f);
[self fadeIn];
}
}

– (void)fadeIn
{
_tableView.transform = CGAffineTransformMakeScale(1.3, 1.3);
_tableView.alpha = 0;
[UIView animateWithDuration:.35 animations:^{
_tableView.alpha = 1;
_tableView.transform = CGAffineTransformMakeScale(1, 1);
}];
}

– (void)fadeOut
{
[UIView animateWithDuration:.15 animations:^{
_tableView.transform = CGAffineTransformMakeScale(1.3, 1.3);
_tableView.alpha = 0.0;
} completion:^(BOOL finished) {
if (finished) {
[_overlayView removeFromSuperview];
[_tableView removeFromSuperview];
}
}];
}

@end

– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];

CCComboBox *comboBox = [[CCComboBox alloc] initWithFrame:CGRectMake(10, 10, 300, 30)];
comboBox.dataArray = _dataArray;
[comboBox setComboBoxBackgroundImage:[UIImage imageNamed:@"select_button_bg"] forState:UIControlStateNormal];
comboBox.tag = 10000;
//[self.window addSubview:comboBox];
comboBox.delegate = self;
NSMutableArray *array = [NSMutableArray arrayWithArray:@[@"–東京都–“, @"新宿区", @"品川区", @"渋谷区", @"中央区", @"千代田区", @"大田区", @"豊島区", @"江東区"]];
//NSMutableArray *array1 = [NSMutableArray arrayWithCapacity:0];
[comboBox setDataArray:array];
UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, comboBox.frame.size.height +20)];
[subView addSubview:comboBox];
subView.backgroundColor = [UIColor grayColor];

CCToggleView *toggleView = [[CCToggleView alloc] initWithSubView:subView subViewVisiable:NO];
[toggleView setToggleBackgroundImage:[UIImage imageNamed:@"toggle_normal"] forState:UIControlStateNormal];
[toggleView setToggleBackgroundImage:[UIImage imageNamed:@"toggle_selected"] forState:UIControlStateSelected];
[self.window addSubview:toggleView];

[self.window makeKeyAndVisible];
return YES;
}

IOS

Posted by arkgame