2017-06-27 9 views
5

Ich versuche, Bild in UITableView Swipe-Stil hinzuzufügen. Ich habe versucht, mit Text Emoji & seine feinenWie Bild in UITableViewRowAction hinzufügen?

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { 
    let editAction = UITableViewRowAction(style: .normal, title: "") { (rowAction, indexPath) in 
     print("edit clicked") 
    } 

    return [editAction] 
} 

arbeitete Aber ich brauche Bild statt Emoji, mittlerweile habe ich versucht,

editAction.backgroundColor = UIColor.init(patternImage: UIImage(named: "edit")!) 

Aber es wird immer doppeltes Bild, habe ich Bilder in vielen Format wie 20 * 20 , 25 * 25, 50 * 50 aber immer noch duplizieren.

Wie kann ich ein Bild hinzufügen?

+0

Mögliche Duplikate von [UITableViewRowAction Bild für Titel] (https://stackoverflow.com/questions/29421894/uitableviewrowaction-image-for-Title) – Pochi

+0

Ich glaube, es kann ein Problem mit der Bildgröße sein, die Sie versuchen zu setzen. Bitte, überprüfen Sie [diese] (https://Stackoverflow.com/a/37452825/4108415) für weitere Informationen. –

+0

@Jack versuchen Mine Lösung Ich hoffe, es löst Ihr Problem. –

Antwort

5

Schließlich in iOS 11, SWIFT 4 Wir fügen können mit Hilfe von UISwipeActionsConfiguration

@available(iOS 11.0, *) 
    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { 

      let action = UIContextualAction(style: .normal, title: "Files", handler: { (action,view,completionHandler) in 
       //do stuff 
       completionHandler(true) 
      }) 
     action.image = UIImage(named: "apple.png") 
     action.backgroundColor = .red 
     let confrigation = UISwipeActionsConfiguration(actions: [action]) 

     return confrigation 
    } 

WWDC video at 28.34

Apple Doc

Hinweis Bild in UITableView der Swipe-Aktion hinzufügen: I 50 verwendet haben * 50 Punkte apple.png Bild mit 50 tableviewrow height

-1
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { 

    let write = UITableViewRowAction(style: .default, title: "\u{1F58A}") { action, index in 
     print("edit button tapped") 
    } 

    return [write] 
} 

versuchen Sie dieses Symbol verwendet Unicode statt. das wird funktionieren

0

Hier ist, wie ich es in objective-c mache und sollte in swift arbeiten, wenn übersetzt.

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    NSString *deleteString = @"Delete"; 
    CGFloat tableViewCellHeight = [self tableView:tableView heightForRowAtIndexPath:indexPath]; 
    UIImage *image = [UIImage imageNamed:@"delete_icon"]; 

    CGFloat fittingMultiplier = 0.4f; 
    CGFloat iOS8PlusFontSize = 18.0f; 
    CGFloat underImageFontSize = 13.0f; 
    CGFloat marginHorizontaliOS8Plus = 15.0f; 
    CGFloat marginVerticalBetweenTextAndImage = 3.0f; 

    float titleMultiplier = fittingMultiplier; 

    NSString *titleSpaceString= [@"" stringByPaddingToLength:[deleteString length]*titleMultiplier withString:@"\u3000" startingAtIndex:0]; 

    UITableViewRowAction *rowAction= [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:titleSpaceString handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){ 
     //Do Stuff 
    }]; 

    CGSize frameGuess=CGSizeMake((marginHorizontaliOS8Plus*2)+[titleSpaceString boundingRectWithSize:CGSizeMake(MAXFLOAT, tableViewCellHeight) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName: [UIFont systemFontOfSize:iOS8PlusFontSize] } context:nil].size.width, tableViewCellHeight); 

    CGSize tripleFrame=CGSizeMake(frameGuess.width*3.0f, frameGuess.height*3.0f); 

    UIGraphicsBeginImageContextWithOptions(tripleFrame, YES, [[UIScreen mainScreen] scale]); 
    CGContextRef context=UIGraphicsGetCurrentContext(); 

    [[UIColor blueColor] set]; 
    CGContextFillRect(context, CGRectMake(0, 0, tripleFrame.width, tripleFrame.height)); 

    CGSize drawnTextSize=[deleteString boundingRectWithSize:CGSizeMake(MAXFLOAT, tableViewCellHeight) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName: [UIFont systemFontOfSize:underImageFontSize] } context:nil].size; 

    [image drawAtPoint:CGPointMake((frameGuess.width/2.0f)-([image size].width/2.0f), (frameGuess.height/2.0f)-[image size].height-(marginVerticalBetweenTextAndImage/2.0f)+2.0f)]; 

    [deleteString drawInRect:CGRectMake(((frameGuess.width/2.0f)-(drawnTextSize.width/2.0f))*([[UIApplication sharedApplication] userInterfaceLayoutDirection]==UIUserInterfaceLayoutDirectionRightToLeft ? -1 : 1), (frameGuess.height/2.0f)+(marginVerticalBetweenTextAndImage/2.0f)+2.0f, frameGuess.width, frameGuess.height) withAttributes:@{ NSFontAttributeName: [UIFont systemFontOfSize:underImageFontSize], NSForegroundColorAttributeName: [UIColor whiteColor] }]; 

    [rowAction setBackgroundColor:[UIColor colorWithPatternImage:UIGraphicsGetImageFromCurrentImageContext()]]; 
    UIGraphicsEndImageContext(); 

    return @[rowAction]; 
} 
0

fand ich eine Möglichkeit, dies in SWIFT zu tun, 3 -

enter image description here

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { 
     //let cell = tableView.cellForRow(at: indexPath) 
     //print(cell?.frame.size.height ?? 0.0)//hence we need this height of image in points. make sure your contentview of image is smaller 
     let deleteAction = UITableViewRowAction(style: .normal, title:"  ") { (rowAction, indexPath) in 
      print("delete clicked") 
     } 
     deleteAction.backgroundColor = UIColor(patternImage:UIImage(named: "delete")!) 
     return [deleteAction] 
    } 

Wir müssen unsere Bilddimension, um sicherzustellen, wird passend mit Zellzeilenhöhe Hier ist mein Bild, das ich verwendete enter image description here

+0

Wie ich in 'UITableViewRowAction' gefunden habe, das in swift3 eingeschränkte Funktionalität hat, kann man es jedoch mit einem Bild versuchen, das eine Hintergrundfarbe hat. – Jack