Eya*_*yal 16 objective-c uibutton uitableview ios
处理自定义按钮的按钮触摸事件的最佳做法是什么UITableViewCell?
我的课:
MyViewController,MyCustomCell
我可以想到三个选择:
第一个选项 -将按钮作为属性MyCustomCell,然后在MyViewController.m文件中添加目标MyViewController作为目标.
MyViewController .m文件
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"customCell";
MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
[cell.theButton addTarget:self
action:@selector(theButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];
}
// Configure the cell...
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)theButtonTapped:(UIButton *)sender
{
MyCustomCell *selectedCell = (MyCustomCell *)sender.superview;
if (selectedCell) {
NSIndexPath *indexPath = [self.tableView indexPathForCell:selectedCell];
MyModel *selectedModel = [self.model objectAtIndex:indexPath.row];
// do something with the model...
}
}
Run Code Online (Sandbox Code Playgroud)
第二个选项 -如果自定义单元格是在IB中创建的,请将nib文件的所有者设置为MyViewController,实现buttonTapped:方法MyViewController并将按钮的Touch Up Inside事件连接到该buttonTapped:方法.
第三个选项 -如果自定义单元格不是在IB中制作的,则将目标添加到MyCustomCell.m文件中的按钮MyCustomCell作为目标.
定义MyCustomCellDelegate添加@property (nonatomic, assign) id<MyCustomCellDelegate> delegate到MyCustomCell,并调用此委托时,按钮被窃听.在创建单元格和实现协议时
设置MyViewController为单元格的委托MyCustomCellDelegate.
MyCustomCell .h文件
@class MyCustomCell;
@protocol MyCustomCellDelegate <NSObject>
- (void)buttonTappedOnCell:(MyCustomCell *)cell;
@end
@interface MyCustomCell : UITableViewCell
@property (nonatomic, retain) UIButton *theButton;
@property (nonatomic, assign) id<MyCustomCellDelegate> delegate;
@end
Run Code Online (Sandbox Code Playgroud)
MyCustomCell .m文件
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.theButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.theButton.frame = CGRectMake(10,10,50,30);
[self addSubview:self.theButton];
[self.theButton addTarget:self
action:@selector(theButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
- (void)theButtonTapped:(UIButton *)sender
{
[self.delegate buttonTappedOnCell:self];
}
Run Code Online (Sandbox Code Playgroud)
MyViewController .m文件
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"customCell";
MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.delegate = self;
}
// Configure the cell...
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)buttonTappedOnCell:(MyCustomCell *)selectedCell
{
if (selectedCell) {
NSIndexPath *indexPath = [self.tableView indexPathForCell:selectedCell];
MyModel *selectedModel = [self.model objectAtIndex:indexPath.row];
// do something with the model...
}
}
Run Code Online (Sandbox Code Playgroud)
Kre*_*iri 12
将单元格行存储为tag自定义按钮的属性.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// bla bla bla
if (!cell)
{
//bla bla bla
[cell.yourButton addTarget:self selector:@selector(yourButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
}
// bla bla bla
cell.yourButton.tag = indexPath.row;
}
-(void)yourButtonTapped:(id)sender
{
int tag = [(UIButton *)sender tag];
NSLog(@"tapped button in cell at row %i", tag);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12377 次 |
| 最近记录: |