¿Cuál es la mejor práctica para manejar un evento táctil de botón para un botón de un UITableViewCell
personalizado?Objetivo c: la mejor práctica para manejar un evento táctil de botón para un botón de una UITableViewCell personalizada
mis clases: MyViewController
, MyCustomCell
que se me ocurren tres opciones:
Primera opción- tienen el botón como una propiedad de MyCustomCell
, y luego añadir un objetivo a ella en el MyViewController
archivo .m con MyViewController
como destino.
MyViewController
archivo .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 opción- Si la celda personalizado se hizo en IB, Ajuste del archivo semilla propietario de ser MyViewController
, implementar buttonTapped:
método en el MyViewController
y conecte el botón Touch Up Inside evento al método buttonTapped:
.
Tercera opción- si la celda personalizado no se hizo en IB, agregue un destino para el botón en el archivo .m MyCustomCell
con MyCustomCell
como objetivo.
Defina MyCustomCellDelegate
agregue @property (nonatomic, assign) id<MyCustomCellDelegate> delegate
a MyCustomCell
y llame a este delegado cuando se toca el botón.
Establezca MyViewController
como el delegado de la celda al crear celdas e implemente el protocolo MyCustomCellDelegate
.
MyCustomCell
archivo .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
archivo .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
archivo .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...
}
}
Me gustaría ir con la opción 1. Parece la más legible para mí. Utilizo un enfoque similar, pero configuro el delegado en la celda personalizada y manejo el botón en la celda personalizada misma y luego invoco una función en el controlador de vista comprobando si el delegado está allí. –