iOS 自定義單選按鈕

iOS 沒有單選按鈕這樣的控件 ,但是項目開發還是會經常用到。電商項目里邊的地址選擇或者支付方式的選擇就經常會用到單選按鈕,增強用戶體驗。


/cell的.h文件/

#import@interface CarCell : UITableViewCell

@property (nonatomic , strong) UILabel *border;

@property (nonatomic , strong) UIButton *radioBtn;

@property (nonatomic , strong) UILabel *defaultL;

@end


/cell的.m文件/

#import "CarCell.h"

@implementation CarCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{

self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

if (self) {

//添加控件

[self addSubviews];

}

return self;

}

- (void)addSubviews

{

self.radioBtn = [UIButton buttonWithType:UIButtonTypeCustom];

self.radioBtn.frame = CGRectMake(SCREEN_WIDTH - 60, 10, 50, 50);

[self.radioBtn setImage:[UIImage imageNamed:@"round_normal@3x"] forState:UIControlStateNormal];

[self.radioBtn setImage:[UIImage imageNamed:@"round@3x"] forState:UIControlStateSelected];

//

self.defaultL = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 60, CGRectGetMaxY(self.radioBtn.frame) - 6, 50, 12)];

self.defaultL.text = @"默認";

self.defaultL.font = [UIFont systemFontOfSize:12];

self.defaultL.textAlignment = NSTextAlignmentCenter;

self.defaultL.textColor = [UIColor colorWithRed:45.0/255.0 green:177.0/255.0 blue:136.0/255.0 alpha:1.0];

self.defaultL.hidden = YES;

[self addSubview:self.radioBtn];

[self addSubview:self.defaultL];

}


/下面是關鍵/

#import "MyGarageViewController.h"@interface MyGarageViewController (){

NSArray *_rowArray;

NSArray *_sectionArray;

}

@property (nonatomic , strong) UITableView *myTableView;

@property (nonatomic , strong) NSMutableArray *selectedArray;

@end

@implementation MyGarageViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

self.view.backgroundColor = [UIColor blackColor];

self.titleLabel.text = @"我的車庫";

//

self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64 + 10, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 10) style:UITableViewStylePlain];

self.myTableView.delegate = self;

self.myTableView.dataSource = self;

self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;

//tableview滑上去之后下不來了,下行代碼可以解決

self.automaticallyAdjustsScrollViewInsets = NO;

self.myTableView.backgroundColor = [UIColor blackColor];

[self.view addSubview:self.myTableView];

_rowArray = [NSArray arrayWithObjects:@"1",@"2",@"3", nil];

_sectionArray = [NSArray arrayWithObjects:@"1", nil];

//初始化單選按鈕數組 ,設置首個為默認按鈕

_selectedArray = [NSMutableArray new];

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

if (i == 0) {

[self.selectedArray setObject:@"1" atIndexedSubscript:0];

}else{

[self.selectedArray setObject:@"0" atIndexedSubscript:i];

}

}

}

#pragma mark - UITableViewDataSource , UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return _sectionArray.count;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return _rowArray.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

CarCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

if (cell == NULL) {

cell = [[CarCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

cell.selectionStyle = UITableViewCellSelectionStyleNone;

cell.separatorInset = UIEdgeInsetsZero;

cell.clipsToBounds = YES;

}

if (indexPath.row == _rowArray.count - 1) {

cell.border.hidden = YES;

}

if (indexPath.row == 0) {

cell.defaultL.hidden = NO;

}

[cell.radioBtn addTarget:self action:@selector(radioBtnAction:) forControlEvents:UIControlEventTouchUpInside];

cell.radioBtn.tag = indexPath.row + 400;

NSLog(@"%@" , self.selectedArray);

NSString *selectstr = [self.selectedArray objectAtIndex:indexPath.row];

if ([selectstr isEqualToString:@"1"]) {

cell.radioBtn.selected = YES;

}else{

cell.radioBtn.selected = NO;

}

return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

return 70;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

NSLog(@"cell%ld" , indexPath.row);

}

- (void)radioBtnAction:(UIButton *)sender

{

self.selectedArray = [NSMutableArray new];

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

NSLog(@"選中按鈕的tag=%ld\n%d" , sender.tag , i );

if (i == sender.tag - 400) {

[self.selectedArray setObject:@"1" atIndexedSubscript:sender.tag - 400];

}else{

[self.selectedArray setObject:@"0" atIndexedSubscript:i];

}

}

[self.myTableView reloadData];

NSLog(@"選中");

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容