gpt4 book ai didi

swift - 更改 tableView 中单元格的 editingStyle 背景颜色和标签

转载 作者:搜寻专家 更新时间:2023-11-01 06:14:14 25 4
gpt4 key购买 nike

我有一个 TableView ,我试图在其中执行滑动删除操作。

我正在使用 swift 4 和 Xcode 9。

到目前为止,我有下一个代码,它允许我在滑动时显示删除标签:

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
switch editingStyle {
case .delete:
print("deleting row")

default:
return
}
}

到目前为止一切顺利,现在我想将背景红色更改为另一种颜色,如果可能的话,我想显示一个图标而不是标签。

我正在尝试我在 Swift 4 中看到的这段代码,但它在创建按钮时出现问题:

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let deleteButton = UITableViewRowAction(style: .default, title: "Delete") { (action, indexPath) in
self.tableView.dataSource?.tableView!(self.tableView, commit: .delete, forRowAt: indexPath)
return
}
deleteButton.backgroundColor = UIColor.black
return [deleteButton]
}

它给出了这个错误 -> 对成员 'tableView(_:numberOfRowsInSection:)' 的引用不明确

我不知道我是在尝试编辑的好方法,还是在 swift 4 上根本无法编辑。

感谢任何帮助,

谢谢!

最佳答案

我正在使用 Xcode 9.2 和 swift 4。使用相同的代码但没有错误。完整代码在这里。这可能对您有所帮助。

    import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

var tableView: UITableView!

override func viewDidLoad() {
super.viewDidLoad()

tableView = UITableView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height), style: .plain)

tableView.dataSource = self
tableView.delegate = self

self.view.addSubview(tableView)
}

func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: "Cell")
cell.textLabel?.text = "Lalala"
return cell
}

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

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
switch editingStyle {
case .delete:
print("deleting row")

default:
return
}
}

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let deleteButton = UITableViewRowAction(style: .default, title: "Delete") { (action, indexPath) in
self.tableView.dataSource?.tableView!(self.tableView, commit: .delete, forRowAt: indexPath)
return
}
deleteButton.backgroundColor = UIColor.black
return [deleteButton]
}
}

关于swift - 更改 tableView 中单元格的 editingStyle 背景颜色和标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48497706/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com