自定义表格视图行操作(图片)

3
我的应用程序包括一个UITableView。它的单元格具有显示多行文本的选项。这些单元格还具有自定义行操作(图像)。我设置行操作图像的方式如下:
let date = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "", handler: { (action, indexPath) -> Void in})    
date.backgroundColor = UIColor(patternImage: UIImage(named: "rowActionPic")!)

这张图片的分辨率是122x94。如果单元格只显示一行,那么图片完美地适应了。但是,如果单元格显示2行或更多行,则该图片将显示2次。是否有选项可以将图片居中显示?

cellForRowAtIndexPath中的代码:

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

你能添加 cellForRowAtIndexPath 的代码吗? - Karlos
我在问题中添加了cellForRowAtIndexPath的代码。希望能有所帮助。如果需要更多信息,请随时提问。 - paro
https://dev59.com/uV0b5IYBdhLWcg3wQPNN#38655903 - Jayesh Miruliya
1个回答

4

自然地,一个图案将被重复以填满所有空间。

我能想象的唯一方法是增加图像的高度,超过最有可能的最大单元格高度。


我使用了这段代码和一个具有透明背景和高度为120px的图像,其中图标占据了前44x44像素。

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)
    }
}

结果:

enter image description here

enter image description here

使用有不透明背景的图片会更好看。

enter image description here


访问GitHub查看示例项目。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接