如何在 UITableView 的编辑模式下将自定义单元格向左移动

Ina*_*ung 5 uitableview swift

我有 UITableView 的自定义单元格,它由 UIImage 和 textLabel 组成。当我选择编辑模式时,textLabel 显示如下图;

在此处输入图片说明

我想把它移到左边。我尝试了所有的事情,但没有奏效。请告诉我。

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return allRows.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{

    let cell = tableView.dequeueReusableCell(withIdentifier: "editCell", for: indexPath) as! CustomTableView

    cell.textLabel?.text = self.allRows[indexPath.row].bname
    cell.textLabel?.textAlignment = .left

    let image:UIImage = UIImage(named: "group")!
    cell.imageView!.image = image

    return cell

}

func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle{

    return .delete
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath){

   if editingStyle == .delete{

            self.allRows.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath as IndexPath], with: .fade)

    }
}

@objc func leftEditButtonPressed(_ sender: UIButton) {
    self.tableView!.isEditing = true
    self.tableView?.setEditing(true, animated: true)

}
Run Code Online (Sandbox Code Playgroud)

bes*_*per 0

尝试使用以下代码:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.dataArr.count
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 80.0
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "TestTableCell") as? TestTableCell else {
        return UITableViewCell()
    }

    cell.customImageView.image = #imageLiteral(resourceName: "ic_audio")
    cell.customTitleLabel.text = self.dataArr[indexPath.row]
    return cell
}

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
}

func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
    return UITableViewCellEditingStyle.delete
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        print("delete at \(indexPath.row)")
    }
}
Run Code Online (Sandbox Code Playgroud)

这是此代码的输出:

在此输入图像描述

注意:您可以更改方法中的编辑操作editingStyleForRowAt,并且要执行该操作,您需要在方法中编写逻辑commit editingStyle

希望这对您有帮助!