【问题标题】:swift 3.0 multiple selection with Select All option带有全选选项的 swift 3.0 多项选择
【发布时间】:2017-09-25 06:01:12
【问题描述】:

我已经在表格视图中添加了数据,并且当用户选择第一个选项(全选)时,我已经手动将“全选”选项添加到列表的第一个位置,然后在选择相同时应该选择和取消选择列表中的所有项目.我已经尝试了下面的代码,但它不起作用所以任何人都可以帮我解决这个问题

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = ObjTableview.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! SelectUserCell
    for i in 0 ..< self.getStudentName.count {
        cell.btnCheckbox.setImage(UIImage(named: "selectedItem"), for: .normal)
        print (i) //i will increment up one with each iteration of the for loop

    }

}
var unchecked = true
 @IBAction func btnCheckBoxClick(_ sender: Any) {

if unchecked == true {
           //(sender as AnyObject).setImage(UIImage(named: "selectedItem"), for: .normal)
            cell?.btnCheckbox.setImage(UIImage(named: "selectedItem"), for: .normal)
            //unchecked = false
            let cello = ObjTableview.cellForRow(at: indexPath!)
            print(cello!)
            ObjTableview.reloadData()
        }else
        {
            //(sender as AnyObject).setImage(UIImage(named: "unSelectedItem"), for: .normal)
            cell?.btnCheckbox.setImage(UIImage(named: "unSelectedItem"), for: .normal)
           // unchecked = true
        }
}

【问题讨论】:

  • 在行的单元格中,您只设置UIImage(named: "selectedItem",为什么 for loop ?仅应用了数组的最后一个值,我可以看到的其他问题是 btnCheckBoxClick 您正在更新 unchecked
  • 在 Array 上创建逻辑。一旦将数据加载到数组中,将全零,当您选择任何单元格时,将该索引值更改为 1。当您在数组中全选时,全为 1。当您取消全选时,全零在数组中。
  • 永远不会cellForRow...之外的表格视图单元格出列。这是一个致命的错误。

标签: ios uitableview swift3


【解决方案1】:

Jayprakash,你快到了。你需要修改一些行 -

这是你修改后的代码 sn-p

var unchecked:Bool = true

@IBAction func btnCheckBoxClick(_ sender: Any) {

    if(unchecked){

        unchecked = false
    }
    else{

        unchecked = true
    }


    ObjTableview.reloadData()


}



func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    if(indexPath.row == 0){

            btnCheckBoxClick(sender: UIButton())

      }

 }


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{

        let cell : SelectUserCell!
        cell = tableView .dequeueReusableCell(withIdentifier: "SelectUserCell", for: indexPath) as! SelectUserCell
        cell.selectionStyle = UITableViewCellSelectionStyle.none


       if(unchecked){

              cell.btnCheckbox.setImage(UIImage(named: "unSelectedItem"), for: .normal)

         }
       else{

              cell.btnCheckbox.setImage(UIImage(named: "selectedItem"), for: .normal)
         }


   // Do your stuff here

   return cell
}

Hop 会简化你的代码结构。

【讨论】:

  • 第一个选择时,所有单元格都被选中,但是当第二个单元格被选中时,所有单元格都被制作,只有我只选择第一个单元格才会这样做
  • @JayprakashSingh 以下是场景: 1. 仅当您选择第一个单元格时才选择所有单元格。那么当你选择其他单元格时会发生什么?
  • 第二个单元格的选择只完成了我从第一个单元格中看到的所有内容
  • 好吧,你的意思是选择 |取消选择所有单元格只会在您第一次选择单元格时执行。
  • @阿米尔汗是的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-22
  • 2015-03-21
  • 2023-04-06
  • 1970-01-01
  • 2018-03-08
  • 2015-08-10
  • 2020-11-15
相关资源
最近更新 更多