2015-11-11 25 views
5

All'interno di un UITableViewCell, sto cercando di implementare un pulsante sia con un'immagine e di testo.Swift: come rendere un UIView cliccabile in UITableViewCell?

Sembra che lo standard UIButton non riesca a ottenere quello. Così ho creato uno UIView che contiene uno UIImageView e uno UILabel.

Potete vedere l'attuazione qui sul lato destro, il tasto "follow viaggio" (il "+" è un UIImageView, e "seguire viaggio" è un UILabel)

enter image description here

I sto cercando ora di rendere tale UIView (cioè il pulsante) cliccabile, ma non riesco a trovare un modo.

Questa è la mia realizzazione, ma non funziona:

class StationsIntroHeader: UITableViewCell { 

    @IBOutlet weak var bigButton: UIView! 

    override func awakeFromNib() { 
     super.awakeFromNib() 
     let tap = UITapGestureRecognizer(target: self, action: Selector("followTrip:")) 
     bigButton.addGestureRecognizer(tap) 
    } 

    func followTrip(sender:UITapGestureRecognizer) { 
     print("tap working") 
    } 
} 

ho fatto in modo che l'interazione Utente Abilitato è ON sul UIView e OFF sia sul UIImageView e UILabel

+2

si può solo fare un arancio con segno più come immagine di sfondo per il pulsante e il diritto di allineare l'etichetta viaggio follow, o anche puoi aggiungere un pulsante personalizzato della stessa dimensione della tua vista e aggiungerlo alla tua vista con sfondo trasparente e senza etichetta in modo da poterlo fare clic su –

+1

I tocchi vengono "assorbiti" da "UITableView". Aggiungi invece un riconoscitore di gesti a 'UITableView' e usa le coordinate per capire quale cella e quale pulsante è stato cliccato. –

+0

per me un'implementazione come la tua è totalmente funzionante! sei sicuro di aver impostato tutto correttamente? –

risposta

4

per me una configurazione di esempio come il seguente tutto funziona:

class TableViewController: UITableViewController { 
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 5 
    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    return tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) 
    } 
} 

class CustomCell: UITableViewCell { 
    @IBOutlet weak var bigButton: UIView! 

    override func awakeFromNib() { 
    super.awakeFromNib() 
    let tap = UITapGestureRecognizer(target: self, action: Selector("bigButtonTapped:")) 
    bigButton.addGestureRecognizer(tap) 
    } 

    func bigButtonTapped(sender: UITapGestureRecognizer) { 
    print("bigButtonTapped") 
    } 
} 

non ho modificato nessuno dei valori predefiniti di userInteractionEnabled per la vista o vista immagine o etichetta. confronta la mia implementazione con la tua e vedi se forse hai dimenticato qualcosa ... per esempio? collegare la presa?

progetto

di esempio: https://www.dropbox.com/sh/hpetivhc3gfrapf/AAAf6aJ0zhvRINPFJHD-iMvya?dl=0

di modifica per il vostro progetto

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    let headerCell = tableView.dequeueReusableCellWithIdentifier("StationsIntroHeader") as! StationsIntroHeader 
    headerCell.update() 
    return headerCell 
    // return headerCell.update().contentView 
} 
+0

Ciao Andrè! Ho appena eseguito il tuo progetto, e funziona davvero! Sto cercando ora di confrontare le impostazioni di Interface Builder dei controlli per individuare eventuali differenze –

+0

Se non trovi ciò che non va, condividi il tuo progetto tramite dropbox - se disponibile - e do un'occhiata! –

+0

Non riesco a trovare la soluzione! Ho caricato una versione semplificata: https://www.dropbox.com/sh/rgfvrn1jpqcfloj/AADEar4aryx08rwb6je5s_mqa?dl=0 –

1

Puoi semplicemente sottoclasse UIButton e implementare il disegno personalizzato al suo interno. UIButton è il modo più semplice per eseguire l'operazione senza interferire con i tocchi della vista tabella. Se implementi il ​​gesto del tocco sulla vista tabella, causerà problemi ai tocchi della cella.

È possibile ottenere semplicemente l'immagine personalizzata (sia + segno che testo come un'immagine) e utilizzarla come immagine di sfondo.

Oppure puoi anche disegnarlo con il codice.

Ad esempio, si può provare questo:

func drawCanvas1(frame frame: CGRect = CGRectMake(3, 8, 209, 109)) { 
    //// General Declarations 
    let context = UIGraphicsGetCurrentContext() 

    //// Color Declarations 
    let color = UIColor(red: 0.967, green: 0.423, blue: 0.211, alpha: 1.000) 

    //// Image Declarations 
    let screenShot20151111At32900PM = UIImage(named: "screenShot20151111At32900PM.png")! 

    //// Rectangle Drawing 
    let rectanglePath = UIBezierPath(roundedRect: CGRectMake(frame.minX + 39, frame.minY + 23, 113, 46), cornerRadius: 8) 
    color.setFill() 
    rectanglePath.fill() 


    //// Rectangle 2 Drawing 
    let rectangle2Rect = CGRectMake(frame.minX + 51, frame.minY + 27, 33, 34) 
    let rectangle2Path = UIBezierPath(rect: rectangle2Rect) 
    CGContextSaveGState(context) 
    rectangle2Path.addClip() 
    screenShot20151111At32900PM.drawInRect(CGRectMake(floor(rectangle2Rect.minX - 16 + 0.5), floor(rectangle2Rect.minY - 15 + 0.5), screenShot20151111At32900PM.size.width, screenShot20151111At32900PM.size.height)) 
    CGContextRestoreGState(context) 


    //// Text Drawing 
    let textRect = CGRectMake(frame.minX + 97, frame.minY + 23, 73, 46) 
    let textTextContent = NSString(string: "\nfollow\ntrip\n") 
    let textStyle = NSParagraphStyle.defaultParagraphStyle().mutableCopy() as! NSMutableParagraphStyle 
    textStyle.alignment = .Left 

    let textFontAttributes = [NSFontAttributeName: UIFont.systemFontOfSize(UIFont.labelFontSize()), NSForegroundColorAttributeName: UIColor.whiteColor(), NSParagraphStyleAttributeName: textStyle] 

    let textTextHeight: CGFloat = textTextContent.boundingRectWithSize(CGSizeMake(textRect.width, CGFloat.infinity), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: textFontAttributes, context: nil).size.height 
    CGContextSaveGState(context) 
    CGContextClipToRect(context, textRect); 
    textTextContent.drawInRect(CGRectMake(textRect.minX, textRect.minY + (textRect.height - textTextHeight)/2, textRect.width, textTextHeight), withAttributes: textFontAttributes) 
    CGContextRestoreGState(context) 
} 

e il risultato sarà qualcosa di simile:

enter image description here