自定义表格视图行动(图片)

我的应用程序包括一个UITableView。 它的单元格可以select显示多行。 单元格也有自定义的行动(图片)。 我像这样设置了行动图像:

let date = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "", handler: { (action, indexPath) -> Void in}) date.backgroundColor = UIColor(patternImage: UIImage(named: "rowActionPic")!) 

该图像的分辨率为122×94。 如果单元格显示一行,则全部适合完美。 如果单元格显示2行或更多行,则图像将显示2次。 有没有select中心的形象?

cellForRowAtIndexPath代码:

 cell.textLabel?.numberOfLines = 0 let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as! NSManagedObject cell.textLabel?.text = object.valueForKey("name") as? String 

自然会重复一个模式来填充所有的空间。

我能成像的唯一方法是增加图像的高度,以超过最可能的最大细胞高度。


我用这个代码和一个透明的背景和120px的图像,其中图标占据第一个44×44像素的图像

 import UIKit class ViewController: UITableViewController { override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 30 } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell cell.textLabel?.text = "\(indexPath.row)" return cell } override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? { let moreClosure = { (action: UITableViewRowAction!, indexPath: NSIndexPath!) -> Void in println("More closure called") } let moreAction = UITableViewRowAction(style: .Normal, title: " ", handler: moreClosure) if let image = UIImage(named: "star.png"){ moreAction.backgroundColor = UIColor(patternImage: image) } return [moreAction] } override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { } override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return min((44.0 * CGFloat(indexPath.row) + 1), 120.0) } } 

结果:

在这里输入图像说明

在这里输入图像说明

随着背景不透明的图像看起来更好。

在这里输入图像说明


访问GitHub作为示例项目。