【问题标题】:Custom table view row action (image)自定义表格视图行操作(图片)
【发布时间】:2015-08-25 14:09:17
【问题描述】:

我的应用包含一个 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的代码吗?
  • 我在问题中添加了cellForRowAtIndexPath的代码。希望能帮助到你。如果您需要更多信息,请随时询问。

标签: ios swift cocoa-touch uitableviewrowaction


【解决方案1】:

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

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


我使用了这段代码和一个透明背景和高度为 120 像素的图像,其中图标占据了前 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)
    }
}

结果:

使用具有不透明背景的图像看起来更好。


访问GitHub 获取示例项目。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-26
    • 1970-01-01
    • 2017-02-28
    • 1970-01-01
    • 1970-01-01
    • 2015-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多