UIViewController的.m文件
#import "ViewController.h"
#import "MyTableViewCell.h"
@interface ViewController ()<UITableViewDataSource,
UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *arrayDS1;
@property (nonatomic, strong) NSMutableArray *arrayDS2;
@property (nonatomic, strong) NSMutableArray *arrayDS3;
@property (nonatomic, strong) UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupDatas];
[self setupSubviews];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)setupDatas {
NSArray *arr1=@[@"唐金鹏",@"秋丽",@"爸爸",@"妈妈",@"姐姐",@"堂哥",@"堂弟",@"表弟",@"占昭"];
NSArray *arr2=@[@"iOS",@"财务",@"普通人",@"普通人",@"销售",@"C++",@"开餐馆",@"Java",@"武汉大学"];
NSArray *arr3=@[@"男",@"女",@"男",@"女",@"女",@"男",@"男",@"男",@"男"];
self.arrayDS1 = [[NSMutableArray alloc] initWithArray:arr1];
self.arrayDS2 = [[NSMutableArray alloc] initWithArray:arr2];
self.arrayDS3 = [[NSMutableArray alloc] initWithArray:arr3];
}
- (void)setupSubviews {
UIView *view1=[[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 30)];
view1.backgroundColor=[UIColor orangeColor];
[self.view addSubview:view1];
UILabel *label1=[[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
label1.text=@"姓名";
label1.textAlignment=NSTextAlignmentCenter;
[view1 addSubview:label1];
UILabel *label2=[[UILabel alloc] initWithFrame:CGRectMake(125, 0, 100, 30)];
label2.text=@"性别";
label2.textAlignment=NSTextAlignmentCenter;
[view1 addSubview:label2];
UILabel *label3=[[UILabel alloc] initWithFrame:CGRectMake(262, 0, 100, 30)];
label3.text=@"职业";
label3.textAlignment=NSTextAlignmentCenter;
[view1 addSubview:label3];
self.automaticallyAdjustsScrollViewInsets = NO;
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64+30, self.view.bounds.size.width, self.view.bounds.size.height-64-30-50) style:UITableViewStylePlain];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView];
self.tableView.tableFooterView = [[UIView alloc] init];//去掉没有内容的cell
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height-50, self.view.bounds.size.width, 50)];
myLabel.text=@"唐金鹏&&秋丽";
myLabel.textAlignment=NSTextAlignmentCenter;
myLabel.font=[UIFont systemFontOfSize:20.f];
myLabel.backgroundColor = [UIColor cyanColor];
[self.view insertSubview:myLabel aboveSubview:self.tableView];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.arrayDS1.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * str = nil;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;//隐藏分割线
str = @"CustomCell";
MyTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:str];
if (cell == nil) {
cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
}
cell.labelName.text = [self.arrayDS1 objectAtIndex:indexPath.row];
cell.labelNum.text = [self.arrayDS2 objectAtIndex:indexPath.row];
if ([[self.arrayDS3 objectAtIndex:indexPath.row] isEqualToString:@"男"]) {
cell.labelsex.textColor=[UIColor blackColor];
} else {
cell.labelsex.textColor=[UIColor redColor];
}
cell.labelsex.text=[self.arrayDS3 objectAtIndex:indexPath.row];
cell.backgroundColor=[UIColor darkGrayColor];
if (indexPath.row%2==0) {
cell.imageView.image=[UIImage imageNamed:@"tab-my-Click@2x.png"];
cell.iconView.image=[UIImage imageNamed:@"home-Click@2x.png"];
} else {
cell.imageView.image=[UIImage imageNamed:@"home-Click@2x.png"];
cell.iconView.image=[UIImage imageNamed:@"tab-my-Click@2x.png"];
}
UIButton *btn1=[[UIButton alloc] initWithFrame:CGRectMake(100, 65, 60, 32)];
btn1.tag = indexPath.row;
btn1.backgroundColor=[UIColor brownColor];
[btn1 setTitle:@"按钮" forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(btn1clicked:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:btn1];
UISwitch *switchView = [[UISwitch alloc]initWithFrame:CGRectMake(260.0f, 65.0f, 100.0f, 28.0f)];
switchView.on = NO;//设置初始为OFF的一边
[switchView addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged]; // 开关事件切换通知
switchView.tag=100+indexPath.row;
[cell addSubview: switchView];
cell.selectionStyle = UITableViewCellSelectionStyleNone;//点击cell时,cell的颜色不改变
return cell;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.tableView reloadData];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100; //cell的高度
}
-(void)btn1clicked:(UIButton *)btn{
NSLog(@"btn1======%ld",(long)btn.tag);
dispatch_async(dispatch_get_main_queue(), ^{
UILabel *theLabel=[[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width-self.view.frame.size.width*3/5)/2, self.view.frame.size.height*2.5/5, self.view.frame.size.width*3/5, 20+self.view.frame.size.height/25)];
theLabel.backgroundColor=[UIColor blackColor];
theLabel.layer.cornerRadius = 5.f;
theLabel.clipsToBounds = YES;
theLabel.textColor=[UIColor whiteColor];
theLabel.alpha=0.8f;
theLabel.textAlignment=NSTextAlignmentCenter;
theLabel.font=[UIFont systemFontOfSize:15.f];
theLabel.text=[NSString stringWithFormat:@"按钮的tag=%ld",(long)btn.tag];
[self.view addSubview:theLabel];
//设置动画
CATransition *transion=[CATransition animation];
transion.type=@"push";//动画方式
transion.subtype=@"fromRight";//设置动画从哪个方向开始
[theLabel.layer addAnimation:transion forKey:nil];//给layer添加动画。设置延时效果
//不占用主线程
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.4 * NSEC_PER_SEC)), dispatch_get_main_queue(),^{
[theLabel removeFromSuperview];
});//这句话的意思是1.5秒后,把label移出视图
});
}
-(void)switchAction:(UISwitch *)theSwitch
{
NSLog(@"theSwitch==%ld",(long)theSwitch.tag);
UISwitch *switchButton = (UISwitch *)theSwitch;
BOOL isButtonOn = [switchButton isOn];
NSString *switchStr;
if (isButtonOn) {
NSLog(@"开");
switchStr=@"开";
}else {
NSLog(@"关");
switchStr=@"关";
}
dispatch_async(dispatch_get_main_queue(), ^{
UILabel *theLabel=[[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width-self.view.frame.size.width*3/5)/2, self.view.frame.size.height*2.5/5, self.view.frame.size.width*3/5, 20+self.view.frame.size.height/25)];
theLabel.backgroundColor=[UIColor blackColor];
theLabel.layer.cornerRadius = 5.f;
theLabel.clipsToBounds = YES;
theLabel.textColor=[UIColor whiteColor];
theLabel.alpha=0.8f;
theLabel.textAlignment=NSTextAlignmentCenter;
theLabel.font=[UIFont systemFontOfSize:15.f];
theLabel.text=[NSString stringWithFormat:@"开关的tag=%ld 开关=%@",(long)theSwitch.tag,switchStr];
[self.view addSubview:theLabel];
//设置动画
CATransition *transion=[CATransition animation];
transion.type=@"push";//动画方式
transion.subtype=@"fromRight";//设置动画从哪个方向开始
[theLabel.layer addAnimation:transion forKey:nil];//给layer添加动画。设置延时效果
//不占用主线程
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.4 * NSEC_PER_SEC)), dispatch_get_main_queue(),^{
[theLabel removeFromSuperview];
});//这句话的意思是1.5秒后,把label移出视图
});
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
写一个类继承自UITableViewCell
.h文件
#import <UIKit/UIKit.h>
@interface MyTableViewCell : UITableViewCell
@property (nonatomic, weak) UIImageView *iconView;//头像
@property (nonatomic, strong) UILabel * labelName;//姓名
@property (nonatomic, strong) UILabel * labelsex; //性别
@property (nonatomic, strong) UILabel * labelNum; //手机号
@end
.m文件
#import "MyTableViewCell.h"
@implementation MyTableViewCell
//重写初始化方法:将控件添加到单元格上,如果将子视图控件添加到cell上 借助contenView视图,这样的话cell上子视图会随着cell的变化而变化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
NSLog(@"cell宽度%f cell高度%f", self.bounds.size.width,self.bounds.size.height);
UIView *myView=[[UIView alloc] initWithFrame:CGRectMake(10, 3, 375-2*10, 100-2*3)];
myView.backgroundColor=[UIColor purpleColor];
[self.contentView addSubview:myView];
//设置圆角边框
myView.layer.cornerRadius = 5;
myView.layer.masksToBounds = YES;
//设置边框及边框颜色
myView.layer.borderWidth = 1;
myView.layer.borderColor =[ [UIColor grayColor] CGColor];
self.labelName = [[UILabel alloc] initWithFrame:CGRectMake(10, (100-30)/2, 100, 30)];
self.labelName.textColor = [UIColor cyanColor];
self.labelName.backgroundColor=[UIColor blackColor];
self.labelName.textAlignment = NSTextAlignmentCenter;
[self.contentView addSubview:self.labelName];
self.labelsex = [[UILabel alloc] initWithFrame:CGRectMake(125, (100-30)/2, 100, 30)];
self.labelsex.textColor = [UIColor redColor];
self.labelsex.backgroundColor=[UIColor yellowColor];
self.labelsex.textAlignment = NSTextAlignmentCenter;
[self.contentView addSubview:self.labelsex];
self.labelNum = [[UILabel alloc] initWithFrame:CGRectMake(262, (100-30)/2, 100, 30)];
self.labelNum.textColor = [UIColor greenColor];
self.labelNum.backgroundColor=[UIColor blueColor];
self.labelNum.textAlignment = NSTextAlignmentCenter;
[self.contentView addSubview:self.labelNum];
UIImageView *iconView = [[UIImageView alloc] init];
iconView.frame=CGRectMake(375/2+32, (100-45)/2, 45, 45);
[self.contentView addSubview:iconView];
self.iconView = iconView;
}
return self;
}
- (void)awakeFromNib {
[super awakeFromNib];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end