如何使用自定义复选框按钮选择表格视图行选择? [英] how to select Table View row selection with custom checkbox button?

查看:26
本文介绍了如何使用自定义复选框按钮选择表格视图行选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用自定义按钮选择 tableview 行.我有另一个按钮,称为选择表视图之外的所有内容,我的问题是在单击表视图按钮外时如何选择和取消选择表视图内的行?同时我可以在 tableview 中选择单行吗?如何在swift 3中做到这一点?这是我在 cellforrow 方法中的代码

How to select tableview row with custom button . i have another button called select all its outside of the table view my question is while clicking outside of the tableview button how to select and deselect inside tableview rows? At the same time i could able to select single row in the tableview ? how to do it in swift 3? This is my code in cellforrow method

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


    let identifier = "Custom"
    var cell: TStudentAttendanceCell! = tableView.dequeueReusableCell(withIdentifier: identifier) as? TStudentAttendanceCell
    if cell == nil {
        tableView.register(UINib(nibName: "TStudentAttendanceCell", bundle: nil), forCellReuseIdentifier: identifier)
        cell = tableView.dequeueReusableCell(withIdentifier: identifier) as? TStudentAttendanceCell
    }
    print("studentAttendanvceArray--",studentAttendanceArray.object(at: indexPath.row) )
    var localDic :NSDictionary!

    localDic = studentAttendanceArray.object(at: indexPath.row) as! NSDictionary

    Common.sharedInstance.StopActivity()        
    cell.profile_img.image = self.image
    cell.name_lbl.text = localDic["student_name"] as? String
    cell.selectionStyle = UITableViewCellSelectionStyle.none
    cell.contentView.backgroundColor = UIColor.clear
    let whiteRoundedView : UIView = UIView(frame: CGRect(x: 10, y: 8, width: self.view.frame.size.width - 20, height: 90))

    whiteRoundedView.layer.backgroundColor = CGColor(colorSpace: CGColorSpaceCreateDeviceRGB(), components: [1.0, 1.0, 1.0, 0.9])
    whiteRoundedView.layer.masksToBounds = false
    whiteRoundedView.layer.cornerRadius = 2.0
    whiteRoundedView.layer.shadowOffset = CGSize(width: -1, height: 1)
    whiteRoundedView.layer.shadowOpacity = 0.2
    cell.contentView.addSubview(whiteRoundedView)
    cell.contentView.sendSubview(toBack: whiteRoundedView)

    return cell

}

推荐答案

视图控制器

class ViewController: UIViewController,UITableViewDelegate, UITableViewDataSource {

  @IBOutlet var tableView: UITableView!
  var allStudentsArr:[[String:String]] = []
  var selectedRows:[IndexPath] = []


  override func viewDidLoad() {
    super.viewDidLoad()
    tableView.allowsSelection = false
    allStudentsArr = [["name":"name1"],["name":"name2"],["name":"name3"],["name":"name4"],["name":"name5"],["name":"name6"],["name":"name7"],["name":"name8"]]
  }
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return allStudentsArr.count
  }

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! CustomTableViewCell
    cell.nameLbl.text = allStudentsArr[indexPath.row]["name"]
    if selectedRows.contains(indexPath)
    {
      cell.checkBox.setImage(UIImage(named:"selected"), for: .normal)
    }
    else
    {
      cell.checkBox.setImage(UIImage(named:"unselected"), for: .normal)
    }
    cell.checkBox.tag = indexPath.row
    cell.checkBox.addTarget(self, action: #selector(checkBoxSelection(_:)), for: .touchUpInside)
    return cell
  }
  @objc func checkBoxSelection(_ sender:UIButton)
  {
    let selectedIndexPath = IndexPath(row: sender.tag, section: 0)
    if self.selectedRows.contains(selectedIndexPath)
    {
      self.selectedRows.remove(at: self.selectedRows.index(of: selectedIndexPath)!)
    }
    else
    {
      self.selectedRows.append(selectedIndexPath)
    }
    self.tableView.reloadData()
  }
  @IBAction func selectAllBtnAction(_ sender: UIBarButtonItem) {
    self.selectedRows = getAllIndexPaths()
    self.tableView.reloadData()
  }

  func getAllIndexPaths() -> [IndexPath] {
    var indexPaths: [IndexPath] = []
      for j in 0..<tableView.numberOfRows(inSection: 0) {
        indexPaths.append(IndexPath(row: j, section: 0))
      }
        return indexPaths
  }
}

自定义单元格

class CustomTableViewCell: UITableViewCell {

  @IBOutlet var nameLbl: UILabel!
  @IBOutlet var checkBox: UIButton!
}

这篇关于如何使用自定义复选框按钮选择表格视图行选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆