【发布时间】:2019-08-24 16:15:11
【问题描述】:
我有一个列出静态数组值的表格视图。可以在表格视图中进行多项选择。我试图在选择单元格时显示 UIView 并隐藏未选择任何单元格,此外我想将选定的单元格值保存到数组中,以便能够将数据发布到 api 调用。
这是我的代码:
//UIView which i want to show (when cell is selected) and hide (when cell is not selected)
@IBOutlet weak var rejAppView: UIView!
//tableView
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sampleData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "prCell", for: indexPath) as! PrListCellTableViewCell
let data = sampleData[indexPath.row]
print("req|:\(String(describing: data.req))")
cell.reqLbl.text = data.req
let ChecktapGesture = UITapGestureRecognizer(target: self, action: #selector(self.checktapBtnAction(_:)))
cell.tickImageView.tag = indexPath.row
cell.tickImageView.addGestureRecognizer(ChecktapGesture)
cell.tickImageView.isUserInteractionEnabled = true
let passReqtapGesture = UITapGestureRecognizer(target: self, action: #selector(self.passReqtapBtnAction(_:)))
cell.passReqNo.tag = indexPath.row
cell.passReqNo.addGestureRecognizer(passReqtapGesture)
cell.passReqNo.isUserInteractionEnabled = true
return cell
}
对于多选,我为 imageView 添加了点击手势:
这里是代码多选和 UIVIiew 显示/隐藏:
@objc func checktapBtnAction(_ sender: UITapGestureRecognizer) {
print("\(String(describing: sender.view?.tag)) Tapped")
guard let rowIndexPath = sender.view?.tag else {
return
}
let indexPath = IndexPath(row: rowIndexPath, section: 0)
let cell = tableView.cellForRow(at: indexPath) as! PrListCellTableViewCell
// if cell.checked == true {
//
// self.rejAppView.isHidden = true
// .
//
// } else if cell.checked == false {
// self.rejAppView.isHidden = false
// }
cell.checked = !cell.checked
print("cell type\(String(describing: cell.checked))")
}
这是我的 tableView 单元格代码:
@IBOutlet weak var tickImageView: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
checked = false
}
var checked: Bool! {
didSet {
if (self.checked == true) {
self.tickImageView.image = UIImage(named: "tick")
}else{
self.tickImageView.image = UIImage(named: "untick")
}
}
}
我的问题是,我可以在选择单个/多个单元格时显示 rejAppView (UIView),但是当取消选择所有单元格时,rejAppView (UIView) 没有被隐藏,我也无法将选定的值存储到数组中。 任何帮助都非常感谢...
【问题讨论】:
标签: ios arrays swift uitableview