带有自定义单元格的 TableView 不会开始编辑
TableView with custom cells doesn't begin editing
我有一个 table 视图,其中包含自定义单元格。这些单元格内部有一些按钮。
当我按下按钮使 table 进入编辑模式时,它什么也没做。
如果我将单元格的 class 设置为 UITableViewCell 那么它就可以工作。
奇怪的是,如果我将 table 视图默认设置为编辑模式,那么当我启动应用程序时,table 处于编辑模式。
唯一的问题是当我按下应该将其设置为编辑模式的按钮时。
编辑按钮:
@objc func didTapEdit(sender: AnyObject){
if table.isEditing == false{
table.isEditing = true
print("okokokok")
} else {
table.isEditing = false
}
}
//This works properly with table views with default cells
Table 设置:
view.addSubview(table)
table.translatesAutoresizingMaskIntoConstraints = false
table.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
table.topAnchor.constraint(equalTo: view.topAnchor, constant: navBarHeight).isActive = true
table.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 30).isActive = true
table.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -30).isActive = true
自定义单元格:
contentView.addSubview(btn)
btn.translatesAutoresizingMaskIntoConstraints = false
btn.topAnchor.constraint(equalTo: topAnchor, constant: topSpace).isActive = true
btn.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -5).isActive = true
btn.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -5).isActive = true
let btnWidth = NSLayoutConstraint(item: btn, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 0.0, constant: bounds.height-10)
btn.addConstraint(btnWidth)
我有多个按相同方式设置的按钮。
那么问题出在哪里呢?我使用自定义单元格创建了其他 table 视图,它们都运行良好。另外,如果我将 table 视图设置为默认处于编辑模式,当我 运行 应用程序处于编辑模式时,我可以四处移动单元格!这里发生了什么?我认为问题可能在于我如何设置约束,但我真的不知道
您将 btn
添加到单元格的 contentView
,就像您 应该 一样。但是随后您将按钮限制为 cell
,您 不应该 这样做。将约束更改为:
contentView.addSubview(btn)
btn.translatesAutoresizingMaskIntoConstraints = false
btn.topAnchor.constraint(equalTo: contentView.topAnchor, constant: topSpace).isActive = true
btn.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -5).isActive = true
btn.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -5).isActive = true
let btnWidth = NSLayoutConstraint(item: btn, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 0.0, constant: bounds.height-10)
btn.addConstraint(btnWidth)
作为旁注,您的 btnWidth
约束看起来很奇怪...如果您希望按钮宽度比单元格高度小 10 磅,您应该能够得到与:
btn.widthAnchor.constraint(equalTo: contentView.heightAnchor, constant: -10)
编辑 -- 这是一个完整的例子...在导航栏中添加一个“编辑”按钮,用于在编辑和不编辑之间切换:
class EditTestTableViewController: UITableViewController {
lazy var editBtn = UIBarButtonItem(barButtonSystemItem: .edit, target: self, action: #selector(self.editTapped(_:)))
lazy var doneBtn = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(self.editTapped(_:)))
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(EditTestCell.self, forCellReuseIdentifier: "cell")
self.navigationItem.rightBarButtonItem = editBtn
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 8
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let c = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! EditTestCell
c.label.text = "\(indexPath)"
return c
}
@objc func editTapped(_ sender: Any) -> Void {
tableView.isEditing.toggle()
self.navigationItem.rightBarButtonItem = tableView.isEditing ? doneBtn : editBtn
}
override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
// handle moving row...
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
// handle delete row...
}
}
和一个简单的自定义单元格 class:
class EditTestCell: UITableViewCell {
let btn = UIButton()
let label = UILabel()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
func commonInit() -> Void {
btn.backgroundColor = .red
btn.setTitle("Test", for: [])
label.backgroundColor = .green
contentView.addSubview(label)
label.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(btn)
btn.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
btn.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 12.0),
btn.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -12.0),
btn.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -5.0),
btn.widthAnchor.constraint(equalTo: contentView.heightAnchor, constant: -10.0),
label.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 5.0),
label.centerYAnchor.constraint(equalTo: contentView.centerYAnchor),
])
}
}
我有一个 table 视图,其中包含自定义单元格。这些单元格内部有一些按钮。
当我按下按钮使 table 进入编辑模式时,它什么也没做。
如果我将单元格的 class 设置为 UITableViewCell 那么它就可以工作。
奇怪的是,如果我将 table 视图默认设置为编辑模式,那么当我启动应用程序时,table 处于编辑模式。
唯一的问题是当我按下应该将其设置为编辑模式的按钮时。
编辑按钮:
@objc func didTapEdit(sender: AnyObject){
if table.isEditing == false{
table.isEditing = true
print("okokokok")
} else {
table.isEditing = false
}
}
//This works properly with table views with default cells
Table 设置:
view.addSubview(table)
table.translatesAutoresizingMaskIntoConstraints = false
table.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
table.topAnchor.constraint(equalTo: view.topAnchor, constant: navBarHeight).isActive = true
table.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 30).isActive = true
table.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -30).isActive = true
自定义单元格:
contentView.addSubview(btn)
btn.translatesAutoresizingMaskIntoConstraints = false
btn.topAnchor.constraint(equalTo: topAnchor, constant: topSpace).isActive = true
btn.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -5).isActive = true
btn.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -5).isActive = true
let btnWidth = NSLayoutConstraint(item: btn, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 0.0, constant: bounds.height-10)
btn.addConstraint(btnWidth)
我有多个按相同方式设置的按钮。
那么问题出在哪里呢?我使用自定义单元格创建了其他 table 视图,它们都运行良好。另外,如果我将 table 视图设置为默认处于编辑模式,当我 运行 应用程序处于编辑模式时,我可以四处移动单元格!这里发生了什么?我认为问题可能在于我如何设置约束,但我真的不知道
您将 btn
添加到单元格的 contentView
,就像您 应该 一样。但是随后您将按钮限制为 cell
,您 不应该 这样做。将约束更改为:
contentView.addSubview(btn)
btn.translatesAutoresizingMaskIntoConstraints = false
btn.topAnchor.constraint(equalTo: contentView.topAnchor, constant: topSpace).isActive = true
btn.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -5).isActive = true
btn.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -5).isActive = true
let btnWidth = NSLayoutConstraint(item: btn, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 0.0, constant: bounds.height-10)
btn.addConstraint(btnWidth)
作为旁注,您的 btnWidth
约束看起来很奇怪...如果您希望按钮宽度比单元格高度小 10 磅,您应该能够得到与:
btn.widthAnchor.constraint(equalTo: contentView.heightAnchor, constant: -10)
编辑 -- 这是一个完整的例子...在导航栏中添加一个“编辑”按钮,用于在编辑和不编辑之间切换:
class EditTestTableViewController: UITableViewController {
lazy var editBtn = UIBarButtonItem(barButtonSystemItem: .edit, target: self, action: #selector(self.editTapped(_:)))
lazy var doneBtn = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(self.editTapped(_:)))
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(EditTestCell.self, forCellReuseIdentifier: "cell")
self.navigationItem.rightBarButtonItem = editBtn
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 8
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let c = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! EditTestCell
c.label.text = "\(indexPath)"
return c
}
@objc func editTapped(_ sender: Any) -> Void {
tableView.isEditing.toggle()
self.navigationItem.rightBarButtonItem = tableView.isEditing ? doneBtn : editBtn
}
override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
// handle moving row...
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
// handle delete row...
}
}
和一个简单的自定义单元格 class:
class EditTestCell: UITableViewCell {
let btn = UIButton()
let label = UILabel()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
func commonInit() -> Void {
btn.backgroundColor = .red
btn.setTitle("Test", for: [])
label.backgroundColor = .green
contentView.addSubview(label)
label.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(btn)
btn.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
btn.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 12.0),
btn.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -12.0),
btn.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -5.0),
btn.widthAnchor.constraint(equalTo: contentView.heightAnchor, constant: -10.0),
label.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 5.0),
label.centerYAnchor.constraint(equalTo: contentView.centerYAnchor),
])
}
}