我创建了一个任务表视图,我希望用户能够在作业完成时向右滑动,然后在作业不完整时再次向右滑动。
在单元格的左侧,我有一个用UIView创建的框,表示单元格是否完整。
当用户每次向右滑动时,我希望能够在绿色(完整)和红色(不完整)之间切换框的颜色。
我不知道实现这个目标的最佳方法?我在stackOverflow中搜索了答案,但看不到任何问题的解决方案。任何帮助将不胜感激!
谢谢。
我的视图控制器:
class ViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Tasks"
tableView.allowsMultipleSelectionDuringEditing = true
tableView.register(jobCell.self, forCellReuseIdentifier: cellId)
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return text.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
let task = text[indexPath.row]
let detailTask = detailText[indexPath.row]
cell.textLabel?.text = task
cell.detailTextLabel?.text = detailTask
return cell
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
if jobComplete == ["true"] {
let jobUncompleteAction = UIContextualAction(style: .normal, title: "Uncomplete", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("Job Complete")
success(true)
})
//jobComplete = false
jobUncompleteAction.backgroundColor = UIColor(red:0.80, green:0.00, blue:0.00, alpha:1.0)
return UISwipeActionsConfiguration(actions: [jobUncompleteAction])
} else {
let jobCompleteAction = UIContextualAction(style: .normal, title: "Complete", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("Job Complete")
success(true)
})
//jobComplete = true
jobCompleteAction.backgroundColor = UIColor(red:0.00, green:0.61, blue:0.02, alpha:1.0)
return UISwipeActionsConfiguration(actions: [jobCompleteAction])
}
}
}
我的tableViewCell:
class jobCell: UITableViewCell {
override func layoutSubviews() {
super.layoutSubviews()
self.textLabel?.frame.origin.x = 20
self.detailTextLabel?.frame.origin.x = 20
}
let jobCompleteBar: UIView = {
let jobCompleteBar = UIView()
jobCompleteBar.backgroundColor = UIColor(red:0.80, green:0.00, blue:0.00, alpha:1.0)
jobCompleteBar.translatesAutoresizingMaskIntoConstraints = false
return jobCompleteBar
}()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
addSubview(jobCompleteBar)
jobCompleteBar.leftAnchor.constraint(equalTo: leftAnchor, constant: 0).isActive = true
jobCompleteBar.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
jobCompleteBar.widthAnchor.constraint(equalToConstant: 3).isActive = true
jobCompleteBar.heightAnchor.constraint(equalToConstant: 100).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
答案 0 :(得分:1)
首先在cellForRow函数中转换你的单元格:
<style></style>
在jobCell中声明一个变量
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! jobCell
从leadingSwipeActionsConfigurationForRowAt
更改jobStatus值var jobStatus: Bool = true {
didSet {
//your color for complete/incomplete
jobCompleteBar.backgroundColor = jobStatus ? .green : .red
}
}