Cel c - Najlepsza praktyka obsługi zdarzenia dotyku przycisku dla przycisku niestandardowego UITableViewCell

Jaka jest najlepsza praktyka obsługi zdarzenia dotyku przycisku dla przycisku niestandardowegoUITableViewCell?

moje zajęcia:MyViewController, MyCustomCell

Mogę wymyślić trzy opcje:

Pierwsza opcja- Miej przycisk jako własnośćMyCustomCell, a następnie dodaj do niego cel wMyViewController .m plik zMyViewController jako cel.

MyViewController plik .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...
    }
}

Druga opcja- Jeśli komórka użytkownika została utworzona w IB, ustaw właściciela pliku nib naMyViewController, wprowadzić w życiebuttonTapped: metoda wMyViewController i podłącz zdarzenie Touch Up Inside przycisku dobuttonTapped: metoda.

Trzecia opcja - jeśli niestandardowa komórka nie została utworzona w IB, dodaj cel do przycisku wMyCustomCell .m plik zMyCustomCell jako cel.
Zdefiniuj aMyCustomCellDelegate Dodaj@property (nonatomic, assign) id<MyCustomCellDelegate> delegate doMyCustomCell i zadzwoń do tego delegata, gdy przycisk zostanie naciśnięty.
ZestawMyViewController jako delegat komórki podczas tworzenia komórek i implementacjiMyCustomCellDelegate protokół.

MyCustomCell .h plik

@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 plik .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 plik .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...
    }
}

questionAnswers(5)

yourAnswerToTheQuestion