kum*_*mar 12 tableview ios swift uitableviewrowaction
从右侧滑动单元格时如何添加自定义图像以删除按钮UITableview
,如下图所示?
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")!) }
Run Code Online (Sandbox Code Playgroud)
从上面的代码中可以看到Image,但它显示为堆叠在一起的一组图像.我的意思是,我无法设置"ContentMode"
到".ScaleToAspectFit"
了"UIImage"
这种方法的最佳方法是什么?
示例代码可能很明显.
基本上,您正在绘制上下文,然后从上下文中检索新图像.
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)
Run Code Online (Sandbox Code Playgroud)
您还可以创建自定义视图(使用UIImageView和UILabel)并在上下文中呈现它.
灵感来自Denny的回答.这是他的代码的objective-c版本.
- (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];
}
Run Code Online (Sandbox Code Playgroud)
以下是图像绘制方法:
- (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;
}
Run Code Online (Sandbox Code Playgroud)
尝试这个
let backImage = UIImageView(image: UIImage(named: "remove"))
backImage.contentMode = .scaleAspectFit
remove.backgroundColor = UIColor(patternImage:backImage.image!)
Run Code Online (Sandbox Code Playgroud)
选择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)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9187 次 |
最近记录: |