Objetivo c - Prática recomendada para manipular um evento de toque de botão para um botão de um UITableViewCell personalizado
Qual é a melhor prática para lidar com um evento de toque de botão para um botão de um personalizadoUITableViewCell
?
minhas aulas:MyViewController
, MyCustomCell
Eu posso pensar em três opções:
Primeira opção- Tenha o botão como uma propriedade deMyCustomCell
e, em seguida, adicione um destino a ele noMyViewController
arquivo .m comMyViewController
como o alvo.
MyViewController
arquivo .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...
}
}
Segunda opçao- Se a célula personalizada foi feita em IB, defina o proprietário do arquivo de ponta para serMyViewController
, implementobuttonTapped:
método emMyViewController
e conecte o evento Touch Up Inside do botão aobuttonTapped:
método.
Terceira opção Se a célula personalizada não foi feita no IB, adicione um alvo ao botão noMyCustomCell
arquivo .m comMyCustomCell
como o alvo.
Definir umMyCustomCellDelegate
adicionar@property (nonatomic, assign) id<MyCustomCellDelegate> delegate
paraMyCustomCell
e chame esse delegado quando o botão tocar.
ConjuntoMyViewController
como o delegado da célula ao criar células e implementar oMyCustomCellDelegate
protocolo.
MyCustomCell
arquivo .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
MyCustomCell
arquivo .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];
}
MyViewController
arquivo .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...
}
}