iOSエージェントは、viewControllerでtableViewセルのbuttonの応答を実現します.
3411 ワード
これは2回目のブログです.内容は前回の代理店に続き、初心者として、この業界の大神たちのように同行者たちに迷いを指摘するためではなく、私の学習過程を記録しただけで、これまで困っていた問題を解決する方法として、文才が悪いので、達人は噴き出さないでください.代理店は最初から全然分かりませんでしたが、今はゆっくり理解して、昨日また小さなプログラムをしました.tableViewのcellにカスタムbuttonがあり、cellのbuttonをクリックすると、controllerでイベントを処理する必要があります.
cell.h
cell.m
viewController.h
これは完全なコードで、とても简単で、しかしその时も自分を困らせて长い间、まさにその言叶に応えて、难しい者はできなくて、できる者は难しくありません...すべての问题はすべてこのようにして、だから难题に出会う时落胆しないでください、更に硬い骨はあなたが口を下ろすことを承知する限り、最后にもかじります...
cell.h
#import
//
@protocol btnClickedDelegate
// cell button , , viewController
-(void)cellBtnClicked:(int)section row:(int)row;
@end
@interface mainTableViewCell : UITableViewCell
// delegate , btnClickedDelegate
@property (nonatomic,weak) id btnDelegate;
@end
cell.m
#import "mainTableViewCell.h"
@implementation mainTableViewCell
-(instancetype)initWithIntNum:(int)section row:(int)row{
if (self == [super init]) {
// NSLog(@"%d",section);
// NSLog(@"%d",row);
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 5, 150, 30)];
label.text = @" ";
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor grayColor];
[self.contentView addSubview:label];
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetMaxX(label.frame)+20, CGRectGetMinY(label.frame), 50, 30)];
[btn setTitle:@" " forState:UIControlStateNormal];
btn.tag = section *100+row+1;
[btn setBackgroundColor: [UIColor colorWithRed: 140/255.0 green:140/255.0 blue:140/255.0 alpha:1]];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:btn];
}
return self;
}
//cell ,
-(void)btnClicked:(UIButton *)btn{
int tag = btn.tag;
int section = tag/100;
int row = tag%100;
// viewController , viewController
[self.btnDelegate cellBtnClicked:section row:row];
}
@end
viewController.h
#import "ViewController.h"
#import "mainTableViewCell.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UITableView *mainTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, [UIScreen mainScreen].bounds.size.width, 240)];
mainTable.backgroundColor = [UIColor redColor];
mainTable.dataSource = self;
mainTable.delegate = self;
[self.view addSubview:mainTable];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 5;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString * cellID = @"cellid";
mainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[mainTableViewCell alloc]initWithIntNum:indexPath.section row:indexPath.row];
// cell
cell.btnDelegate =self;
}
return cell;
}
// ,
-(void)cellBtnClicked:(int)section row:(int)row{
NSLog(@" %d %d ",section,row);
}
これは完全なコードで、とても简単で、しかしその时も自分を困らせて长い间、まさにその言叶に応えて、难しい者はできなくて、できる者は难しくありません...すべての问题はすべてこのようにして、だから难题に出会う时落胆しないでください、更に硬い骨はあなたが口を下ろすことを承知する限り、最后にもかじります...