gpt4 book ai didi

ios - 如何在 iOS Swift 中将选定的 tableView 单元格保存到数组中?

转载 作者:行者123 更新时间:2023-11-28 13:29:28 25 4
gpt4 key购买 nike

我有一个列出静态数组值的 TableView 。可以在tableview中进行多选。我试图在选择单元格时显示 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) 未隐藏,我也无法将所选值存储到数组中。非常感谢任何帮助...

最佳答案

定义 indexPath 数组或整数数组,如果选择了单元格,您可以在其中添加 indexPath/indexPath.row。并在取消选择的单元格上删除该对象。

基于那个数组你可以隐藏 View

var array = [IndexPath]()


@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)


cell.checked = !cell.checked
print("cell type\(String(describing: cell.checked))")

if cell.checked {
array.append(indexPath)
} else {
let index = array.index(of: indexPath)
array.remove(at: index)
}

if array.count == 0 {
self.rejAppView.isHidden = true
} else {
self.rejAppView.isHidden = false
}
}

关于ios - 如何在 iOS Swift 中将选定的 tableView 单元格保存到数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57639594/

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