以编程方式更改 UIImage 位置

Changing the UIImage position programmarically

我有一个带有部分的 UITableView。我在每个单元格中添加了一个 UImage,但它在左侧,我如何更改它以使其转到右侧?

这是我对每个单元格的代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cells")
    cell?.textLabel?.text = data[indexPath.section].subType[indexPath.row]
    cell?.textLabel?.textAlignment = .right
    cell?.textLabel?.font = UIFont(name: "GESSTwoLight-Light", size: 15)
    cell!.isUserInteractionEnabled = false;
    cell!.imageView?.image = UIImage(named: "username.png")

    return cell!
}

This is my UITableView

您只需在 tableView(_:cellForRowAt:) 方法中执行以下操作:

cell.transform = CGAffineTransform(scaleX: -1.0, y: 1.0)
cell.textLabel?.transform = CGAffineTransform(scaleX: -1.0, y: 1.0)
cell.imageView?.transform = CGAffineTransform(scaleX: -1.0, y: 1.0)

这将创建 following

致谢 liau-jian-jie for sharing this solution,也可应用于 UIButtons。