我尝试自定义UITableViewRowAction按钮,因此我使用的是image而不是默认按钮。
我的问题是单元格高度是动态的,所以如果单元格的高度太大,UITableViewRowAction中的图像会重复!,如下所示:
那么如何保持图像始终居中,无论细胞高度如何?
我的代码是:
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
//delete location
let deleteAction: UITableViewRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: " ", handler: {(tableViewRowAction:UITableViewRowAction, index:NSIndexPath) -> Void in
self.deleteFavouriteLocationFromList(indexPath.row)
})
let image:UIImage = UIImage(named: "delete_icon_cell")!
image.imageWithRenderingMode(UIImageRenderingMode.Automatic)
let size = CGSizeApplyAffineTransform(image.size, CGAffineTransformMakeScale(0.29, 0.3))
let hasAlpha = false
let scale: CGFloat = 0.0 // Automatically use scale factor of main screen
UIGraphicsBeginImageContextWithOptions(size, !hasAlpha, scale)
image.drawInRect(CGRect(origin: CGPointZero, size: size))
let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
deleteAction.backgroundColor = UIColor(patternImage: scaledImage)
//edit location name
let editAction: UITableViewRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: " ", handler: {(tableViewRowAction:UITableViewRowAction, index:NSIndexPath) -> Void in
self.presentEditFavouriteLocationNamePopUp(indexPath.row)
})
let editImage:UIImage = UIImage(named: "edit_icon_cell")!
UIGraphicsBeginImageContextWithOptions(size, !hasAlpha, scale)
editImage.drawInRect(CGRect(origin: CGPointZero, size: size))
let editScaledImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
editAction.backgroundColor = UIColor(patternImage: editScaledImage)
return [deleteAction,editAction]
}
感谢。