Custom table view row action (image)

后端 未结 1 517
余生分开走
余生分开走 2021-01-06 21:14

My app includes an UITableView. It\'s cells has the option to display more than one line. The cells has also a custom row action (image). I set the row action image like thi

相关标签:
1条回答
  • 2021-01-06 22:05

    naturally a pattern will be repeated to fill all space.

    The only way I could imaging is to increase height of the image to exceed the most probable maximum cell height.


    I used this code and an image with transparent background and height of 120px in which the icon occupies the first 44x44 px

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

    result:

    With an image that has a opaque background it looks better.


    visit GitHub for an example project.

    0 讨论(0)
提交回复
热议问题