UISwitch 控件
自定义 cell
首先现在viewDidLoad中声明这个 cell
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:@"SKUserRelativesTableViewCell" bundle:nil] forCellReuseIdentifier:@"SKUserRelatives"];
}
只在在代理方法中设置重用标示符
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"SKUserRelatives";
SKUserRelativesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[SKUserRelativesTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:identifier];
}
cell.textLabel.text = @"测试";
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
自己创建自己的 cell
.h
#import <UIKit/UIKit.h>
@interface SKUserRelativesTableViewCell : UITableViewCell{
UILabel *userName;
UISwitch *switch_1;
UISwitch *switch_2;
}
@end
.m
<span style="font-size:18px;">#import "SKUserRelativesTableViewCell.h"
@interface SKUserRelativesTableViewCell ()
@end
@implementation SKUserRelativesTableViewCell
-(void)setSelected:(BOOL)selected animated:(BOOL)animated{
[super setSelected:selected animated:animated];
}
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self layoutLabel];
}
return self;
}
-(void)layoutLabel{
userName = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, 100, 100)];
[self.contentView addSubview:userName];
switch_1 = [[UISwitch alloc]initWithFrame:CGRectMake(SKScreenWidth / 2 + 15 ,5 , 100, 100)];
[switch_1 addTarget:self action:@selector(change:) forControlEvents:(UIControlEventTouchUpInside)];
[self.contentView addSubview:switch_1];
switch_2 = [[UISwitch alloc]initWithFrame:CGRectMake(SKScreenWidth * 3 / 4 + 15,5 ,100, 100)];
[switch_2 addTarget:self action:@selector(change:) forControlEvents:(UIControlEventTouchUpInside)];
[self.contentView addSubview:switch_2];
}
-(void)change:(id)sender{
UISwitch *switch_BT = (UISwitch *)sender;
BOOL isON = [switch_BT isOn];
if (isON) {
NSLog(@"yes");
}
else{
NSLog(@"no");
}
}
@end</span>
更多精彩文章,尽在我的公众号.