UItableViewCell中有button处理
2015-06-18 10:45
375 查看
1.UITableViewCell 中有button
采取的办法一般有:
A.将事件放在cell里面,用Delegate传到VC中
#import <UIKit/UIKit.h>
@class CustomCell;
#define kCellIDentifier @"customCell"
@protocol CustomBtnDelegate <NSObject>
- (void)buttonPressWithCell:(CustomCell *)cell;
@end
@interface CustomCell :
UITableViewCell
@property (weak,
nonatomic) id<CustomBtnDelegate>delegate;
- (void)loadData:(NSString *)title;
- (IBAction)buttonPress:(UIButton *)sender
{
if (self.delegate && [self.delegate
respondsToSelector:@selector(buttonPressWithCell:)]) {
[self.delegate
buttonPressWithCell:self];
}
}
B.将btn的事件通过Block传出去
cell.h中
@class CustomCell;
typedef void(^ButtonActionBlock)(CustomCell *cell);
- (void)loadData:(NSString *)title action:(ButtonActionBlock)actionBlock;
.m中
- (void)loadData:(NSString *)title action:(ButtonActionBlock)actionBlock
{
_action = actionBlock;
_titleLabel.text = title;
[_selectBtn
setTitle:title forState:UIControlStateNormal];
}
- (IBAction)buttonPress:(UIButton *)sender
{
_action(self);
}
C.将Btn事件直接拖到VC中
- (IBAction)btnPress:(UIButton *)sender forEvent:(UIEvent *)event
{
UITouch *touch = [[event
allTouches] anyObject];
CGPoint point = [touch
locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView
indexPathForRowAtPoint:point];
}
采取的办法一般有:
A.将事件放在cell里面,用Delegate传到VC中
#import <UIKit/UIKit.h>
@class CustomCell;
#define kCellIDentifier @"customCell"
@protocol CustomBtnDelegate <NSObject>
- (void)buttonPressWithCell:(CustomCell *)cell;
@end
@interface CustomCell :
UITableViewCell
@property (weak,
nonatomic) id<CustomBtnDelegate>delegate;
- (void)loadData:(NSString *)title;
- (IBAction)buttonPress:(UIButton *)sender
{
if (self.delegate && [self.delegate
respondsToSelector:@selector(buttonPressWithCell:)]) {
[self.delegate
buttonPressWithCell:self];
}
}
B.将btn的事件通过Block传出去
cell.h中
@class CustomCell;
typedef void(^ButtonActionBlock)(CustomCell *cell);
- (void)loadData:(NSString *)title action:(ButtonActionBlock)actionBlock;
.m中
- (void)loadData:(NSString *)title action:(ButtonActionBlock)actionBlock
{
_action = actionBlock;
_titleLabel.text = title;
[_selectBtn
setTitle:title forState:UIControlStateNormal];
}
- (IBAction)buttonPress:(UIButton *)sender
{
_action(self);
}
C.将Btn事件直接拖到VC中
- (IBAction)btnPress:(UIButton *)sender forEvent:(UIEvent *)event
{
UITouch *touch = [[event
allTouches] anyObject];
CGPoint point = [touch
locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView
indexPathForRowAtPoint:point];
}
相关文章推荐
- Texas Instruments matrix-gui-2.0 hacking -- menubar.php
- ant打包jar的build.xml配置
- IOS开发限制UITextField只可以输入数字
- POJ 2299 Ultra-QuickSort【树状数组 ,逆序数】
- Texas Instruments matrix-gui-2.0 hacking -- helper_functions.php
- Texas Instruments matrix-gui-2.0 hacking -- generate.php
- Texas Instruments matrix-gui-2.0 hacking -- index.php
- 特征学习“Building High-level Features Using Large Scale Unsupervised Learning”
- Texas Instruments matrix-gui-2.0 hacking -- json.txt
- CoreBlueTootch的实现
- UIModalPresentationFormSheet resizing view
- VBoxGuestAdditions.iso下载
- 对象转数组 (用json_encode 和 json_decode(,TRUE)
- 本地通知 UILocalNotification的使用
- Access restriction: The type FtpClient is not accessible due to restriction on required library
- String Stringfuffer Stringbuilder
- iOS8开发~UI布局(二)storyboard中autolayout和size class的使用详解
- easyUI datagrid 查询方法
- Android应用程序UI硬件加速渲染环境初始化过程分析
- 笔记: c开发gui程序 (WM_CREATE, WS_CLIPCHILDREN , SetWindowPos)