如何使用自定义按钮选择tableview行。我有一个名为select all all of the table of view view的另一个按钮我的问题是在tableview按钮外面单击如何选择和取消选择tableview行内部?同时我可以在tableview中选择单行?如何在swift 3中做到这一点?这是我在cellforrow方法中的代码
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
}
答案 0 :(得分:2)
<强>的ViewController 强>
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!
}
答案 1 :(得分:1)
这就是你如何以编程方式选择单个部分的所有行
@IBAction func didTapSelectAllButton(sender: UIButton) {
let totalRows = tableView.numberOfRows(inSection: 0)// Make some logic if you have more than 1 section
for row in 0..<totalRows {
let indexPath = IndexPath(row: row, section: 0)
tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
}
}
如果您不想使用tableView
的默认复选框,请从tableView
禁用multipleSelection,并使用额外的全局数组实现逻辑。
var selectedArrayIndex = [Int]()
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if selectedArrayIndex.contains(indexPath.row) {
selectedArrayIndex.remove(at: selectedArrayIndex.index(of: indexPath.row)!)
}else {
selectedArrayIndex.append(indexPath.row)
}
}
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if selectedArrayIndex.contains(indexPath.row) {
// Enable You Check
cell.checkBoxView.isHidden = false
}else {
cell.checkBoxView.isHidden = true
}
}
@IBAction func didTapSelectAllButton(sender: UIButton) {
let totalRows = tableView.numberOfRows(inSection: 0)// Make some logic if you have more than 1 section
selectedArrayIndex.removeAll()
for row in 0..<totalRows {
selectedArrayIndex.append(row)
}
}