【问题标题】:How to transform Delete option in Tableview cell如何在 Tableview 单元格中转换删除选项
【发布时间】:2019-11-26 11:10:29
【问题描述】:

我已经使用How to populate UITableView from the bottom upwards? post 转换了 tableview 和 tableview 单元格以从下到上填充单元格。

但我也想将滑动转换为删除选项。 详情请参考图片。

你可以看到Delete 选项并没有像tableview 那样变换。 任何有用的答案将不胜感激。

【问题讨论】:

标签: ios swift uitableview cgaffinetransform


【解决方案1】:

viewDidLoad中的第一个反向UITableView

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

然后反转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]
    }

在这里editActionsForRowAt 我做了一个工作来打印带有镜像文本的图像

【讨论】:

  • 我尝试了与您的代码相同的方法,但没有成功。请查看此图片imgur.com/2kOWkDr
  • @GautamSareriya,我已经对其进行了测试,现在它正在工作
  • 只是对 cellForRowAt 方法中的一个小修正来转换单元格,仅使用 cell.contentView.transform = CGAffineTransform(scaleX: 1, y: -1) 其余代码将相同。它会起作用。谢谢:)
  • 我已经更正了错误,请标记为正确答案让其他人知道
  • 它仅适用于 1 到 3 行消息单元格。但是,如果消息单元格增加超过 3 行,则它将无法正常工作。请查看图片imgur.com/BIxPElG
【解决方案2】:

转换表格视图

tableView.transform = CGAffineTransform(rotationAngle: -(.pi))

然后在单元格内

cell.transform = CGAffineTransform(rotationAngle: .pi)

【讨论】:

  • 嗨,没有按照你的代码工作,我试过了。可以查看图片imgur.com/4rc9ynI
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-01
相关资源
最近更新 更多