2010-10-21 17 views
23

Sto provando a creare una vista personalizzata per il mio footerView UITableView che contiene un pulsante. Posso vederlo lì, ma quando lo tocco, non succede nulla ... si può vedere che cosa c'è che non va qui:UITableView footerView con pulsante, il pulsante non funziona

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    if(self.tableView.tableFooterView == nil) { 
     //allocate the view if it doesn't exist yet 
     UIView *footerView = [[UIView alloc] init]; 

     //create the button 
     UIButton *addNewBtn = [UIButton buttonWithType:UIButtonTypeCustom];  
     //the button should be as big as a table view cell 
     [addNewBtn setFrame:CGRectMake(0, 0, 320, [AddMerchantTVC cellHeight])]; 

     //set title, font size and font color 
     [addNewBtn setTitle:@"Add New" forState:UIControlStateNormal]; 
     [addNewBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:20]]; 
     [addNewBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
     [addNewBtn addTarget:self action:@selector(addNew:) forControlEvents:UIControlEventTouchUpInside]; 

     UIImageView *cellGradient = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellGradient.png"]]; 
     cellGradient.frame = CGRectMake(0, 54, 320, 16); 
     [footerView addSubview:cellGradient]; 

     //add the button to the view 
     [footerView addSubview:addNewBtn]; 
     footerView.userInteractionEnabled = YES; 
     self.tableView.tableFooterView = footerView; 
     self.tableView.tableFooterView.userInteractionEnabled = YES; 

     [footerView release]; 
     [cellGradient release]; 
} 

- (void)addNew:(id)sender { 
    // This is never called 
    NSLog(@"Touched."); 
} 

risposta

36

È' non impostare il frame del footerView. Impostare la cornice del footerView almeno all'altezza del pulsante altrimenti i tocchi non vengono trasmessi al pulsante.

+0

E 'stato. Grazie! –

+1

Questo ha funzionato per me quando ho implementato '- (CGFloat) tableView: (UITableView *) tableView heightForFooterInSection: (NSInteger) section; 'per specificare l'altezza del piè di pagina. –

+0

Questo ha funzionato anche per me, grazie! –

-8

creare una sottoclasse di UIView e comprendono questi metodi:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { 
    return [super hitTest:point withEvent:event]; 
} 

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event { 
    return [super pointInside:point withEvent:event]; 
} 
+0

Ho davvero bisogno di una sottoclasse di UIView per raggiungere questo obiettivo ? Se è così, è fastidioso. –

0

è necessario impostare sectionFooterHeight proprietà del tableView prima della chiamata a viewForFooterInSection si verifica (non può farlo all'interno del metodo viewForFooterInSection).

+0

Specialmente se usi XIB per costruire il tuo UIView, devi implementare questa funzione –

7
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { 
    return 50.0f; 
} 

mettere questo metodo nella classe il problema si risolve .....

1

sua ha lavorato per me ho solo aggiungere

footerView.frame =CGRectMake(0, 300, 100, 100); 
Problemi correlati