Search code examples
iosswiftuitableviewcgaffinetransform

How to transform Delete option in Tableview cell


I've transformed the tableview and tableview cell to fill the cell from bottom to top using How to populate UITableView from the bottom upwards? post.

But I want to transform the swipe to Delete option as well. Please refer the image for more detail.

enter image description here

You can see the Delete option is not transform as the tableview. Any helpful answer will be very appreciated.


Solution

  • First reverse UITableView in viewDidLoad

    override func viewDidLoad() {
            super.viewDidLoad()
            tableView.transform = CGAffineTransform(scaleX: 1, y: -1)
    }
    

    Then reverse the cell in cellForRowAt.

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                guard let cell = tableView.dequeueReusableCell(withIdentifier: "MyTableViewCell", for: indexPath) as? MyTableViewCell else { fatalError() }
    
                cell.transform = CGAffineTransform(scaleX: 1, y: -1)
                cell.contentView.transform = CGAffineTransform(scaleX: 1, y: -1)
    
                return cell
            }
        func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
            let delete = UITableViewRowAction(style: .default, title: nil) { (action, indexPath) in
                // delete item at indexPath
            }
    
            let label = UILabel(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: 75, height: tableView.rectForRow(at: indexPath).height)))
            label.numberOfLines = 0
            label.textAlignment = .center
            label.textColor = UIColor.white
            label.backgroundColor = UIColor.red
            label.text = "Delete"
            UIGraphicsBeginImageContextWithOptions(label.bounds.size, false, 0.0)
            label.layer.render(in: UIGraphicsGetCurrentContext()!)
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
    
    
    
            if let cgImage = image?.cgImage {
                let rotada3 = UIImage(cgImage: cgImage, scale: image!.scale, orientation: .downMirrored)
                delete.backgroundColor = UIColor(patternImage: rotada3)
    
            }
    
    
            return [delete]
        }
    

    Here on editActionsForRowAt I did a work around to print image with mirrored text