#pragma mark*******创建UITableView*******
-(void)createTableView{
UITableView *table = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style: UITableViewStylePlain];
table.delegate = self;
table.dataSource = self;
[self.view addSubview:table];
}
#pragma mark*******设置其它参数************
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 0) {
return 200;
}
return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
switch (indexPath.section ) {
case 0:
{
NSString *messageID = @"ID";
Practice_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:messageID];
if (!cell) {
cell = [[Practice_TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:messageID];
}
cell.backgroundColor = [UIColor blueColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
break;
case 1:
{
NSString *messageID = @"IDD";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:messageID];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:messageID];
}
cell.backgroundColor = [UIColor blueColor];
return cell;
}
break;
case 2:
{
NSString *messageID = @"IDDD";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:messageID];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:messageID];
}
cell.backgroundColor = [UIColor blueColor];
return cell;
}
break;
default:
break;
}
return nil;
}
Practice_TableViewCell.h
#import
@interface Practice_TableViewCell : UITableViewCell
// 定义Block
@property(nonatomic,copy)void(^buttonBlock)(UIButton *button , UITableViewCell *cell);//这里很重要,一会正向传button的
这里我为什么还在Block中定义 cell呢?这样理解吧,因为是自定义cell,目的是在cell上添加其它东西,所以得把cell和要添加的东西写在一起啊
@end
Practice_TableViewCell.m
@implementation Practice_TableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
for (int i=0; i<2; i++) {
UIButton *buttonxx = [UIButton buttonWithType:UIButtonTypeCustom];
buttonxx.frame = CGRectMake(50+(100+50)*i, 50, 100, 100);
buttonxx.layer.cornerRadius = 50;
buttonxx.tag = 10+i;
[buttonxx addTarget:self action:@selector(btnBlock:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:buttonxx];
}
UIButton *button00 = (UIButton *)[self.contentView viewWithTag:10];
button00.backgroundColor = [UIColor redColor];
[button00 setTitle:@"我要变" forState:UIControlStateNormal];
UIButton *button01 = (UIButton *)[self.contentView viewWithTag:11];
button01.backgroundColor = [UIColor yellowColor];
}
return self;
}
-(void)btnBlock:(UIButton *)sender{
NSLog(@"点击的是%ld",sender.tag);
self.buttonBlock(sender, self);//这里是在点击button的时候传值,另一个目的也是让它能在cell上响应
}
if (!cell) {
cell = [[LianXi_TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:messageID];
}
cell.buttonBlock = ^(UIButton *button , UITableViewCell *cell){
switch (button.tag) {
case 10:
{
Next_ViewController *next = [[Next_ViewController alloc]init];
[self.navigationController pushViewController:next animated:YES];
}
break;
case 11:
{
//这里暂时不写
}
break;
default:
break;
}
};
cell.backgroundColor = [UIColor blueColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
Next_ViewController.h
#import
@interface Next_ViewController : UIViewController
@property(nonatomic, copy)void(^imageJumpBlock)(UIButton *imageJump);//定义
@end
Next_ViewController.m
#import "Next_ViewController.h"
#import "ViewController.h"
@interface Next_ViewController ()
{
UIButton *buttonModel;
}
@end
@implementation Next_ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UILabel *labelChange = [[UILabel alloc]initWithFrame:CGRectMake(20, 250, CGRectGetWidth([UIScreen mainScreen].bounds)-40, 50)];
labelChange.backgroundColor = [UIColor redColor];
labelChange.text = @"你猜我变了吗?";
labelChange.textAlignment = NSTextAlignmentCenter;
labelChange.font = [UIFont systemFontOfSize:45];
[self.view addSubview:labelChange];
buttonModel = [UIButton buttonWithType:UIButtonTypeCustom];
buttonModel.frame = CGRectMake(100, 150, CGRectGetWidth([UIScreen mainScreen].bounds)-200, 50);
buttonModel.backgroundColor = [UIColor redColor];
[buttonModel setTitle:@"返回" forState:UIControlStateNormal];
[buttonModel addTarget:self action:@selector(returnAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonModel];
}
-(void)returnAction:(UIButton *)sender{
[self.navigationController popViewControllerAnimated:YES];
self.imageJumpBlock(buttonModel);//这是第二个页面传button的地方
}
buttonModel传到ViewController 的next.imageJumpBlock = ^(UIButton *imageJum){实现};里面,但是为了看到传值的效果,我把
self.imageJumpBlock(buttonModel);放到了button的触发方法里面
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
switch (indexPath.section ) {
case 0:
{
NSString *messageID = @"ID";
LianXi_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:messageID];
if (!cell) {
cell = [[LianXi_TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:messageID];
}
cell.buttonBlock = ^(UIButton *button , UITableViewCell *cell){
switch (button.tag) {
case 10:
{
Next_ViewController *next = [[Next_ViewController alloc]init];
[self.navigationController pushViewController:next animated:YES];
next.imageJumpBlock = ^(UIButton *imageJum){
[button setBackgroundImage:[UIImage imageNamed:@"000"] forState:UIControlStateNormal];
记住@“000”我这里是给button 添加的一张图片,不要忘了
};//这括号里面的就是第二个页面 传过来的值 用来实现的
}
break;
case 11:
{
//这里暂时不写
}
break;
default:
break;
}
};
cell.backgroundColor = [UIColor blueColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
break;