2016-04-07 7 views
8

Come aggiungere un'immagine personalizzata al pulsante Elimina quando si fa scorrere la cella da destra su UITableview come mostrato nell'immagine sottostante?Come aggiungere l'immagine all'azione di riga in Visualizza come tabella?

enter image description here

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? { 

    let remove = UITableViewRowAction(style: .Default, title: "\nRemove") { action, indexPath in } 
    remove.backgroundColor = UIColor(patternImage: UIImage(named: "remove")!) } 

dal codice precedente, Immagine sta mostrando ma si mostra come un gruppo di immagini accatastati insieme. Voglio dire, non sono in grado di impostare "ContentMode" su per "UIImage"

Qual è il modo migliore per questo approccio?

codice di esempio potrebbe essere apprezzabile.

+1

https://github.com/MortimerGoro/MGSwipeTableCell dovrebbe essere utile –

+0

risposto qui - https://stackoverflow.com/questions/44771778/how-to-add-image-in-uitableviewrowaction/45301272#45301272 – Jack

risposta

4

provare questo

let backImage = UIImageView(image: UIImage(named: "remove")) 
backImage.contentMode = .scaleAspectFit 
remove.backgroundColor = UIColor(patternImage:backImage.image!) 

Scelta-2

var img: UIImage = UIImage(named: "remove") 
var imgSize: CGSize = tableView.frame.size 
UIGraphicsBeginImageContext(imgSize) 
img.drawInRect(CGRectMake(20, 0, 20, 20)) 
var newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext() 
UIGraphicsEndImageContext() 
remove.backgroundColor = UIColor(patternImage: newImage) 
+0

Grazie Anbu. Ho già provato il primo approccio, ma non lo uso. Ora, mentre proviamo il secondo approccio mostra l'errore "Valore di tipo" UITableViewRowAction "non ha un membro" frame "'. Ho capito che dobbiamo dare l'altezza e la larghezza dell'azione. – kumar

+0

non possiamo usare l'altezza 'tableView', poiché è molto più grande della riga destra? – kumar

+0

@kumar - ok scelta n. 2 funziona correttamente, se sì andiamo per il prossimo passo –

4

In sostanza, si sta disegnando in un contesto, quindi recuperare la nuova immagine dal contesto.

let label = UILabel(frame: CGRect(x: 0, y: 0, width: 80, height: 100)) 
label.text = "ADD" 
label.textAlignment = .Center 
label.textColor = UIColor.whiteColor() 

let img: UIImage = UIImage(named: "delete") 

UIGraphicsBeginImageContextWithOptions(imgSize, false, UIScreen.mainScreen().scale) 
let context = UIGraphicsGetCurrentContext() 

// Set background color 
CGContextSetFillColorWithColor(context, UIColor.raangeRed().CGColor) 
CGContextFillRect(context, CGRect(x: 0, y: 0, width: 80, height: 80)) 

// Draw image 
img.drawInRect(CGRectMake(30, 15, 20, 20)) 

// Draw View 
label.layer.renderInContext(context!) 

// Retrieve new UIImage 
let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext() 
UIGraphicsEndImageContext() 

let deleteRowAction = UITableViewRowAction(style: .Normal, title: "   ") { (rowAction, indexPath) in 
    // Do stuff 
    } 
deleteRowAction.backgroundColor = UIColor(patternImage: newImage) 

È inoltre possibile creare una vista personalizzata (con un UIImageView e UILabel) e rendere all'interno del contesto.

+0

Wow, ottimo esempio! Grazie per aver mostrato come utilizzare CGContext per disegnare un'etichetta e applicare il colore di sfondo! Proprio quello di cui avevo bisogno – daisura99

+0

Anche se l'etichetta sembra leggermente "sfocata" e non dà una bella grafica. Funzionerebbe sicuramente meglio se l'immagine contiene anche il testo. – daisura99

+0

Esattamente, dipende anche dal tuo designer, se entrambi siete d'accordo se i pulsanti saranno dinamici o meno –

3

Ispirato alla risposta di Denny. Ecco la versione obiettivo-c del suo codice.

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"   " handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) 
    { 
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Do you really want to delete this comment?" message:@"" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil]; 

     [alertView setTag:101]; 
     [alertView show]; 
    }]; 

    UITableViewCell *commentCell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath]; 

    CGFloat height = commentCell.frame.size.height; 

    UIImage *backgroundImage = [self deleteImageForHeight:height]; 

    deleteAction.backgroundColor = [UIColor colorWithPatternImage:backgroundImage]; 

    return @[deleteAction]; 
} 

E sotto è il metodo di disegno immagine:

- (UIImage*)deleteImageForHeight:(CGFloat)height{ 

    CGRect frame = CGRectMake(0, 0, 62, height); 

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(62, height), NO, [UIScreen mainScreen].scale); 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); 
    CGContextFillRect(context, frame); 

    UIImage *image = [UIImage imageNamed:@"icon-delete"]; 

    [image drawInRect:CGRectMake(frame.size.width/2.0, frame.size.height/2.0 - 10, 18, 20)]; 

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 

    UIGraphicsEndImageContext(); 

    return newImage; 
} 

e il risultato è: Table view cell delete image

Problemi correlati