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(@"选中");
}