Ziel c - Bewährte Vorgehensweise zum Behandeln eines Tastenberührungsereignisses für eine Taste einer benutzerdefinierten UITableViewCell

Was ist die beste Vorgehensweise, um ein Tastenberührungsereignis für eine benutzerdefinierte Taste zu behandeln?UITableViewCell?

meine Klassen:MyViewController, MyCustomCell

Ich kann mir drei Möglichkeiten vorstellen:

Erste Wahl- Hab den Button als Eigentum vonMyCustomCellund füge dann ein Ziel hinzu in derMyViewController .m Datei mitMyViewController als das Ziel.

MyViewController M-Datei

- (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...
    }
}

Zweite Option- Wenn die benutzerdefinierte Zelle in IB erstellt wurde, legen Sie den Besitzer der NIB-Datei auf festMyViewControllerimplementierenbuttonTapped: Methode inMyViewController und verbinde das Touch Up Inside Event des Buttons mit dembuttonTapped: Methode.

Dritte Option- Wenn die benutzerdefinierte Zelle nicht in IB erstellt wurde, fügen Sie der Schaltfläche in der ein Ziel hinzuMyCustomCell .m Datei mitMyCustomCell als das Ziel.
Definiere aMyCustomCellDelegate hinzufügen@property (nonatomic, assign) id<MyCustomCellDelegate> delegate zuMyCustomCell und rufen Sie diesen Delegaten an, wenn Sie auf die Schaltfläche tippen.
einstellenMyViewController als Delegierter der Zelle beim Erstellen von Zellen und Implementieren derMyCustomCellDelegate Protokoll.

MyCustomCell .h Datei

@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 M-Datei

- (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 M-Datei

- (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...
    }
}

Antworten auf die Frage(5)

Ihre Antwort auf die Frage