【问题标题】:How to save selected tableView cell into array in iOS Swift?如何在 iOS Swift 中将选定的 tableView 单元格保存到数组中?
【发布时间】: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


    【解决方案1】:

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

    基于该数组,您可以隐藏视图

    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
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-22
      • 2015-05-31
      • 1970-01-01
      • 2020-01-14
      • 1970-01-01
      • 2014-12-30
      • 2015-04-20
      相关资源
      最近更新 更多