Created
June 2, 2020 09:19
-
-
Save leoiphonedev/ed9e2ee8720bef8fc92be90de3c01962 to your computer and use it in GitHub Desktop.
How to maintain checkbox state when scrolling uitableview
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// addCheckBoxonTable-Tutorial | |
// | |
// Created by Aman Aggarwal on 2/1/18. | |
// Copyright © 2018 iostutorialjunction.com. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController, UITableViewDataSource { | |
@IBOutlet weak var tblList: UITableView! | |
var selectedRows = [IndexPath]() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
tblList.dataSource = self | |
} | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return 10 | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "CheckBoxCell") | |
if let lbl = cell?.contentView.viewWithTag(1) as? UILabel { | |
lbl.text = "item-\(1)" | |
} | |
if let btnChk = cell?.contentView.viewWithTag(2) as? UIButton { | |
btnChk.addTarget(self, action: #selector(checkboxClicked(_ :)), for: .touchUpInside) | |
btnChk.isSelected = false | |
if selectedRows.contains(indexPath) { | |
btnChk.isSelected = true | |
} | |
} | |
return cell! | |
} | |
@objc func checkboxClicked(_ sender: UIButton) { | |
sender.isSelected = !sender.isSelected | |
let point = sender.convert(CGPoint.zero, to: tblList) | |
let indxPath = tblList.indexPathForRow(at: point) | |
if selectedRows.contains(indxPath) { | |
selectedRows.remove(at: selectedRows.index(of: indxPath)!) | |
} else { | |
selectedRows.append(indxPath) | |
} | |
tblList.reloadRows(at: [indxPath], with: .automatic) | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
} |
hi.
could you give me the whole project, please?
Please check this link https://iostutorialjunction.com/2018/02/add-custom-checkbox-on-uitableviewcell.html
Hi, I have exactly follow your code. But when I scroll up after selecting any checkbox its automatically unchecked and any random check box is selected. Could you please explain this behaviour ?
You need to maintain state of checked boxes, you can view this tutorials on
YouTube
Create CheckBox in swift 4 using Xcode 9 0 onwards
https://youtu.be/S6q5d3RvVY8
On Sun, 20 Feb 2022 at 9:51 AM, mawaddat ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
Hi, I have exactly follow your code. But when I scroll up after selecting
any checkbox its automatically unchecked and any random check box is
selected. Could you please explain this behaviour ?
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/ed9e2ee8720bef8fc92be90de3c01962#gistcomment-4071655>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACFA4HYSWOXH5FM22ROE3J3U4BT5PANCNFSM4VRNHDQQ>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
You are receiving this because you authored the thread.Message ID: <leoiphonedev/Saving
checkbox state in ***@***.***>
--
*Thanks and Regards*
*Aman AggarwalSr. iPhone Developer*
*Web:*www.iostutorialjunction.com
*Please don't print this e-mail unless you really need to.*
*Confidential Notice:*
This e-mail transmission and any documents, files, or previous e-mail
messages appended or attached to it, may contain information that is
confidential or legally privileged. If you are not the intended recipient,
or a person responsible for delivering it to the intended recipient, you
are hereby notified that you must not read this transmission and that any
disclosure, copying, printing, distribution, or use of the information
contained or attached to this transmission is STRICTLY PROHIBITED. If you
have received this transmission in error
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi.
could you give me the whole project, please?