技术文章分类(180)

技术随笔(11)

UITableViewCell带有button,获取对应事件,以及传递参数

 

完整demo下载:https://github.com/MartinLi841538513/UITableViewCellButton

 

首先假设你已经会了自定义UITableViewCell(不会的请参考:http://www.cnblogs.com/MartinLi841538513/articles/3785765.html)

现在的情况是,给UITableViewCell写一个代理协议,对每一个button事件写一个代理方法,完了让UITableView所在的Controller实现代理方法。

给cell编写代理协议,以及调用

调用(button事件调用,并传入参数为button,也可以传入其他参数,比如说:self等):

 

在controller中继承和实现

 

全部代码:

Cell.h

#import <UIKit/UIKit.h>

@protocol WithButtonTableViewCellDelegate <NSObject>

-(void)buttonAction:(id)sender;

@end

@interface WithButtonTableViewCell : UITableViewCell

@property (nonatomic,retain)id<WithButtonTableViewCellDelegate>delegate;
@property (weak, nonatomic) IBOutlet UIButton *button;

@end

Cell.m

#import "WithButtonTableViewCell.h"

@implementation WithButtonTableViewCell

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

//触发事件
- (IBAction)buttonAction:(id)sender {
    [self.delegate buttonAction:sender];
}

@end

 

Controller.h

#import <UIKit/UIKit.h>
#import "WithButtonTableViewCell.h"

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,WithButtonTableViewCellDelegate>

@end

Controller.m

#import "ViewController.h"

#define DeviceFrame [UIScreen mainScreen].bounds
#define StatusBarFrame [[UIApplication sharedApplication] statusBarFrame]
#define NavigationBarFrame self.navigationController.navigationBar.frame
#define TabBarFrame self.tabBarController.tabBar.frame

@interface ViewController ()
{
    __weak IBOutlet UITableView *tableview;
    NSString *identifier;
}
@end

@implementation ViewController

-(void)loadView{
    [super loadView];
    self.automaticallyAdjustsScrollViewInsets = NO;
    tableview.frame = CGRectMake(0, StatusBarFrame.size.height+NavigationBarFrame.size.height, DeviceFrame.size.width, DeviceFrame.size.height-StatusBarFrame.size.height-NavigationBarFrame.size.height-50);
    tableview.delegate = self;
    tableview.dataSource = self;
    identifier = @"WithButtonTableViewCell";
    UINib *nib = [UINib nibWithNibName:@"WithButtonTableViewCell" bundle:nil];
    [tableview registerNib:nib forCellReuseIdentifier:identifier];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma UITableViewDatasource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 15;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    int row = indexPath.row;
    WithButtonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.delegate = self;
    cell.button.tag = row;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44.0f;
}

#pragma WithButtonTableViewCellDelegate
-(void)buttonAction:(id)sender{
    UIButton *button = (UIButton *)sender;
    int row = button.tag;
    NSLog(@"button:%d",row);
}

@end

 

posted @ 2014-08-12 14:44  坤哥MartinLi  阅读(556)  评论(0)    收藏  举报