Ios 希望附件复选标记仅在点击右侧时显示
我有一个带有单元格的TableView,当按下单元格中的任意位置时,它会在右侧添加一个复选标记。我只希望在右边点击电池时,复选标记才会出现。以下是TableViewController中的相关代码部分:Ios 希望附件复选标记仅在点击右侧时显示,ios,uitableview,Ios,Uitableview,我有一个带有单元格的TableView,当按下单元格中的任意位置时,它会在右侧添加一个复选标记。我只希望在右边点击电池时,复选标记才会出现。以下是TableViewController中的相关代码部分: override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeu
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TaskCell", for: indexPath) as! TaskCell
let task = tasks[indexPath.row]
cell.task = task
if task.completed {
cell.accessoryType = UITableViewCellAccessoryType.checkmark;
} else {
cell.accessoryType = UITableViewCellAccessoryType.none;
}
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: false)
var tappedItem = tasks[indexPath.row] as Task
tappedItem.completed = !tappedItem.completed
tasks[indexPath.row] = tappedItem
tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.none)
}
}
有没有一个简单的方法来做到这一点,或者使用故事板?我的敏捷技能还有很多需要改进的地方。任何帮助都将不胜感激!谢谢大家! 为什么不提供一个用户可以点击并显示选中标记的实际按钮作为附件视图,而不是内置的选中标记附件类型?例如,按钮通常可能显示为空圆圈,当用户点击按钮时,按钮可能显示为带有复选标记的圆圈 否则,您希望用户猜测一个模糊的界面,而通过这种方式,您点击此处将任务标记为已完成是非常明显的 例如: 为此,我创建了一个button子类,并将每个单元格的
accessoryView
设置为它的一个实例:
class CheckButton : UIButton {
convenience init() {
self.init(frame:CGRect.init(x: 0, y: 0, width: 20, height: 20))
self.layer.borderWidth = 2
self.layer.cornerRadius = 10
self.titleLabel?.font = UIFont(name:"Georgia", size:10)
self.setTitleColor(.black, for: .normal)
self.check(false)
}
func check(_ yn:Bool) {
self.setTitle(yn ? "✔" : "", for: .normal)
}
override init(frame:CGRect) {
super.init(frame:frame)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
按钮的标题可以是空字符串或复选标记字符,从而在点击按钮时显示效果。此代码来自cellForRowAt:
:
if cell.accessoryView == nil {
let cb = CheckButton()
cb.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
cell.accessoryView = cb
}
let cb = cell.accessoryView as! CheckButton
cb.check(self.rowChecked[indexPath.row])
(其中,
rowChecked
是一个Bool数组)。为什么不在附件视图中提供一个用户可以点击并显示复选标记的实际按钮,而不是内置的复选标记附件类型?例如,按钮通常可能显示为空圆圈,当用户点击按钮时,按钮可能显示为带有复选标记的圆圈
否则,您希望用户猜测一个模糊的界面,而通过这种方式,您点击此处将任务标记为已完成是非常明显的
例如:
为此,我创建了一个button子类,并将每个单元格的accessoryView
设置为它的一个实例:
class CheckButton : UIButton {
convenience init() {
self.init(frame:CGRect.init(x: 0, y: 0, width: 20, height: 20))
self.layer.borderWidth = 2
self.layer.cornerRadius = 10
self.titleLabel?.font = UIFont(name:"Georgia", size:10)
self.setTitleColor(.black, for: .normal)
self.check(false)
}
func check(_ yn:Bool) {
self.setTitle(yn ? "✔" : "", for: .normal)
}
override init(frame:CGRect) {
super.init(frame:frame)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
按钮的标题可以是空字符串或复选标记字符,从而在点击按钮时显示效果。此代码来自cellForRowAt:
:
if cell.accessoryView == nil {
let cb = CheckButton()
cb.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
cell.accessoryView = cb
}
let cb = cell.accessoryView as! CheckButton
cb.check(self.rowChecked[indexPath.row])
(其中,
rowChecked
是一个Bool数组)。您必须定义自己的附件按钮,并处理它自己的点击
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TaskCell", for: indexPath) as! TaskCell
let task = tasks[indexPath.row]
cell.task = task
let checkButton = UIButtonSubclass()
...configure button with your circle and check images and a 'selected property'...
checkButton.addTarget(self, action:#selector(buttonTapped(_:forEvent:)), for: .touchUpInside)
cell.accessoryView = checkButton
checkButton.selected = task.completed //... this should toggle its state...
return cell
}
func buttonTapped(_ target:UIButton, forEvent event: UIEvent) {
guard let touch = event.allTouches?.first else { return }
let point = touch.location(in: self.tableview)
let indexPath = self.tableview.indexPathForRow(at: point)
if let task = tasks[indexPath.row] {
task.completed = !task.completed
}
tableView.reloadData() //could also just reload the row you tapped
}
不过,已经注意到,如果您开始删除行,则使用标记来检测点击了哪一行是危险的。你可以在这里读更多
编辑的
根据@matt删除对标签的引用,您必须定义自己的附件按钮,并处理自己的点击
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TaskCell", for: indexPath) as! TaskCell
let task = tasks[indexPath.row]
cell.task = task
let checkButton = UIButtonSubclass()
...configure button with your circle and check images and a 'selected property'...
checkButton.addTarget(self, action:#selector(buttonTapped(_:forEvent:)), for: .touchUpInside)
cell.accessoryView = checkButton
checkButton.selected = task.completed //... this should toggle its state...
return cell
}
func buttonTapped(_ target:UIButton, forEvent event: UIEvent) {
guard let touch = event.allTouches?.first else { return }
let point = touch.location(in: self.tableview)
let indexPath = self.tableview.indexPathForRow(at: point)
if let task = tasks[indexPath.row] {
task.completed = !task.completed
}
tableView.reloadData() //could also just reload the row you tapped
}
不过,已经注意到,如果您开始删除行,则使用标记来检测点击了哪一行是危险的。你可以在这里读更多
编辑的
根据@matt删除对标记的引用我很想这样做,事实上我真的很喜欢圆圈而不是方框,但不幸的是我不知道如何通过编程实现这一点。改进了代码,使复选标记按钮更加封装。我的弱编程能力再次困扰着我!我在CellForRow下有3个错误:1。在cb.addTarget行中,“使用未解析标识符'touchUpInside'”2。在cb.addTarget行中,“使用未解析标识符'Buttontapted'”3。“ListTableViewController”类型的值没有成员“rowChecked”“我想我需要在某个地方设置rowChecked数组?但我不知道如何解决错误1和错误2。您还需要有一个方法
func按钮标记。我刚刚订购了您的书!希望它能帮我解决这个问题。谢谢马特,我想你以前在这方面帮助过我;你是一个伟大的资源!我很想做到这一点,事实上我真的很喜欢圆形而不是长方体,但不幸的是我不知道如何通过编程实现这一点。改进了代码,使复选标记按钮更加封装。我的弱编程能力再次困扰着我!我在CellForRow下有3个错误:1。在cb.addTarget行中,“使用未解析标识符'touchUpInside'”2。在cb.addTarget行中,“使用未解析标识符'Buttontapted'”3。“ListTableViewController”类型的值没有成员“rowChecked”“我想我需要在某个地方设置rowChecked数组?但我不知道如何解决错误1和错误2。您还需要有一个方法func按钮标记。我刚刚订购了您的书!希望它能帮我解决这个问题。谢谢马特,我想你以前在这方面帮助过我;你是一个伟大的资源!的确然后不要显示标签的这种用法,因为它确实是危险的。我是这样做的(这将在您的按钮中显示)
:var r:UIView=sender;重复{r=r.superview!}同时!(r为UITableViewCell);让ip=self.tableView.indepath(for:r as!UITableViewCell)
现在我们有了所需的索引路径,可以根据需要获取其行
。Fair point@matt-认为我不太喜欢迭代UIKit视图层次结构,因此我不再那么懒惰,而是键入了我通常如何处理它!同样公正的观点。但是天哪,我一直在迭代视图/响应器层次结构。如果你做不到这一点,他们到底是干什么的?毕竟,这只是一个链表。超快速、超高效、久经考验且真实。谢谢你!我收到错误“使用未解析标识符'event'”。知道怎么解决吗?@BobF.-对不起,请查看我的编辑。我用错了按钮的签名
。然后不要显示标签的这种用法,因为它确实是危险的。我是这样做的(这将在您的按钮中显示)
:var r:UIView=sender;重复{r=r.superview!}同时!(r为UITableViewCell);让ip=self.tableView.indepath(for:r as!UITableViewCell)
现在我们有了所需的索引路径,可以根据需要获取其行
。Fair point@matt-认为我不太喜欢迭代UIKit视图层次结构,因此我不再那么懒惰,而是键入了我通常如何处理它!同样公正的观点。但是天哪,我在视图中迭代