I have a TableView which rows contain label and two buttons. What I wanna do is that when a user clicks the first button "Set Name", a pop up view comes up in which he can input text from keyboard. After hitting "Set", pop up view is dismissed and label inside a row containing the clicked button changes to the input text. I set the delegates but I cannot make label to change.
TableView:
import UIKit
class SetGame: UIViewController, UITableViewDelegate, UITableViewDataSource
{
var numOfPlayers = Int()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return numOfPlayers
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
cell.Name.text = "Player \(indexPath.row + 1)"
cell.btn1.tag = indexPath.row
cell.btn2.tag = indexPath.row
return cell
}
override func viewDidLoad()
{
super.viewDidLoad()
self.tableView.separatorStyle = UITableViewCellSeparatorStyle.none
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
#IBAction func setName(sender: UIButton)
{
let thisVC = storyboard?.instantiateViewController(withIdentifier: "SetName") as! SetName
thisVC.delegate = self
present(thisVC, animated: true, completion: nil)
}
#IBAction func setFingerprint(_ sender: UIButton)
{
}
#IBAction func unwindToSetGame(_ segue: UIStoryboardSegue)
{
print("unwinded to SetGame")
}
#IBOutlet weak var tableView: UITableView!
}
extension SetGame: nameDelegate
{
func named(name: String)
{
let indexP = IndexPath(row: 0, section: 0)
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexP) as! TableViewCell
cell.Name.text = "bkjhvghcjhkv"
//wanted to see if it changes first cell. But doesn't work
}
}
TableViewCell Class:
import UIKit
class TableViewCell: UITableViewCell
{
override func awakeFromNib()
{
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool)
{
super.setSelected(selected, animated: animated)
}
#IBOutlet weak var Name: UILabel!
#IBOutlet weak var btn1: UIButton!
#IBOutlet weak var btn2: UIButton!
}
Pop up View:
import UIKit
protocol nameDelegate
{
func named(name: String)
}
class SetName: UIViewController
{
var delegate: nameDelegate!
override func viewDidLoad()
{
super.viewDidLoad()
window.layer.borderWidth = 1
window.layer.borderColor = UIColor.white.cgColor
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
#IBAction func closePopUp(_ sender: Any)
{
if input.text != ""
{
delegate.named(name: input.text!)
}
dismiss(animated: true, completion: nil)
}
#IBOutlet weak var input: UITextField!
#IBOutlet weak var window: UIView!
}
Replace this
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexP) as! TableViewCell
with
let cell = tableView.cellForRow(at:indexP) as! TableViewCell
Related
This is my ViewController2 from where I need to pass the data.
How do I pass the data?
protocol oppo {
func datapass(Name:String,className:String,rollnumner:String, school:String)
}
class ViewController2: UIViewController {
#IBOutlet weak var txtFldNames: UITextField!
#IBOutlet weak var txtFldClasss: UITextField!
#IBOutlet weak var txtFldRoll: UITextField!
#IBOutlet weak var txtFldSchool: UITextField!
var datatrsfer:oppo!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func actionAdd(_ sender: UIButton) {
datatrsfer.datapass(Name: txtFldNames.text!, className: txtFldClasss.text!, rollnumner: txtFldRoll.text!, school: txtFldSchool.text!)
}
}
// this is main viewcontoller
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
var dataArry = NSMutableArray()
#IBOutlet weak var tblView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func actionPush(_ sender: UIButton) {
let vc = storyboard?.instantiateViewController(identifier: "ViewController2") as! ViewController2
vc.datatrsfer = self
self.navigationController?.pushViewController(vc, animated: true)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
dataArry.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
return cell
}
}
class TableViewCell: UITableViewCell,oppo {
#IBOutlet weak var txtFldName: UITextField!
#IBOutlet weak var txtFldClass: UITextField!
#IBOutlet weak var txtFldRoll: UITextField!
#IBOutlet weak var txtFldScholl: UITextField!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func datapass(Name: String, className: String, rollnumner: String, school: String) {
txtFldName.text = Name
txtFldClass.text = className
txtFldRoll.text = rollnumner
txtFldScholl.text = school
}
}
import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,oppo {
let nam = NSMutableArray()
#IBOutlet weak var tblView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func actionPush(_ sender: UIButton) {
let vc = storyboard?.instantiateViewController(identifier: "ViewController2") as! ViewController2
vc.datatrsfer = self
self.navigationController?.pushViewController(vc, animated: true)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return nam.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
if let dict = nam[indexPath.row] as? NSMutableDictionary {
cell.txtFldName.text = dict["name"] as? String
cell.txtFldClass.text = dict["classname"] as? String
cell.txtFldRoll.text = dict["rollnumber"] as? String
cell.txtFldScholl.text = dict["school"] as? String
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 300
}
func datapass(Name: String, className: String, rollnumner: String, school: String) {
let dict = NSMutableDictionary()
dict.setValue(Name, forKey: "name")
dict.setValue(className, forKey: "classname")
dict.setValue(rollnumner, forKey: "rollnumber")
dict.setValue(school, forKey: "school")
nam.add(dict)
tblView.reloadData()
}
}
Iv made a NIB custom cell file but I fail to display the custom cell file in my tableView at all. The code is error free and runs but I'm struggling to display the Custom Cell. I Initially thought my problem was from the Unwind Segue that I created to pass the data from the InputScreen to the ViewController but I'm just not sure what the issue could be at this point?
Any help what's so ever would be much appreciated.
MAIN VIEWCONTROLLER
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
var IzParcelz = [IzParcel]()
override func viewDidLoad() {
super.viewDidLoad()
let nib = UINib(nibName: "IzparcelCell", bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "IzparcelCell")
tableView.delegate = self
tableView.dataSource = self
}
// MARK: - TableView data source
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return IzParcelz.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "IzparcelCell", for: indexPath) as! IzparcelCell
let izPackage = IzParcelz[indexPath.row]
cell.nameLabel.text = izPackage.name
cell.addressLabel.text = izPackage.address
cell.trackingNumLabel.text = izPackage.trackingNumber
return cell
}
// MARK: - TableView Delegate
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
IzParcelz.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
IzParcel.saveIzParcel(IzParcelz)
}
}
#IBAction func prepareForUnwind(segue: UIStoryboardSegue) {
guard segue.identifier == "saveUnwind" else { return }
let sourceViewController = segue.source as! InputViewController
if let P = sourceViewController.izParcel {
if let selectedIndexPath = tableView.indexPathForSelectedRow {
IzParcelz[selectedIndexPath.row] = P
tableView.reloadRows(at: [selectedIndexPath], with: .none)
} else {
let newIndexPath = IndexPath(row: IzParcelz.count, section: 0)
IzParcelz.append(P)
tableView.insertRows(at: [newIndexPath], with: .automatic)
}
}
IzParcel.saveIzParcel(IzParcelz)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetails" {
let izViewController = segue.destination as! InputViewController
let indexPath = tableView.indexPathForSelectedRow!
let selectedTodo = IzParcelz[indexPath.row]
izViewController.izParcel = selectedTodo
}
}
}
INPUT VIEWCONTROLLER
import UIKit
class InputViewController: UITableViewController {
var izParcel: IzParcel?
#IBOutlet weak var statusTextLabel: UITextField!
#IBOutlet weak var nameTextLabel: UITextField!
#IBOutlet weak var addressTextLabel: UITextField!
#IBOutlet weak var trackingNumTextLabel: UITextField!
#IBOutlet weak var notesTextLabel: UITextField!
#IBOutlet weak var statusUpdateTextLabel: UIDatePicker!
#IBOutlet weak var dateAndTimeTextLabel: UIDatePicker!
#IBOutlet weak var saveButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
if let izParcels = izParcel {
nameTextLabel.text = izParcels.name
addressTextLabel.text = izParcels.address
trackingNumTextLabel.text = izParcels.trackingNumber
notesTextLabel.text = izParcels.notes
dateAndTimeTextLabel.date = izParcels.dateAndTime
statusUpdateTextLabel.date = izParcels.statusUpdated
}
}
#IBAction func deleteButton(_ sender: UIButton) {
self.dismiss(animated: true, completion: nil)
}
#IBAction func saveButton(_ sender: UIButton) {
print("PressedButtttonnnnTesst")
self.performSegue(withIdentifier: "saveUnwind", sender: self)
}
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard segue.identifier == "saveUnwind" else { return }
let name = nameTextLabel.text!
let address = addressTextLabel.text!
let trackingNumber = trackingNumTextLabel.text!
let notes = notesTextLabel.text!
let dates = dateAndTimeTextLabel.date
let statusUpdate = statusUpdateTextLabel.datePickerMode
izParcel = IzParcel(name: name, address: address, trackingNumber: trackingNumber, notes: notes, dateAndTime: dates, statusUpdated: dates)
}
}
IZPARCEL CELL
import UIKit
class IzparcelCell: UITableViewCell {
#IBOutlet weak var nameLabel: UILabel!
#IBOutlet weak var addressLabel: UILabel!
#IBOutlet weak var trackingNumLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
[]
All the code I had was correct except for the order of the code. After reviewing and reallocating the prepare for segue declaration that was inside the saveButton Action to just outside it. I was able to pass the data from the InputScreen to the Custom TableView Cell in the TableView on the mainScreen.
I am trying to instantiate empty Buyer cells (custom cell) in my table view and then have the user populate the buyers' names. When the user presses the delete button for a row/cell, it should delete the corresponding row/cell regardless of whether or not the textfield for that row has been populated or not. Clearly, I am not getting the desired behavior. For example, when I press delete Row0 (whose textfield says "Buyer 0") and the tableview reloads, Buyer 0 is still there, but one of the empty Buyer cells at the end gets deleted instead.
import UIKit
class EntryAlertViewController: UIViewController {
//Fields/Table
#IBOutlet weak var itemField: UITextField!
#IBOutlet weak var priceField: UITextField!
#IBOutlet weak var tableView: UITableView!
//Visual Components
#IBOutlet weak var mainView: UIView!
#IBOutlet weak var titleView: UIView!
#IBOutlet weak var splitItemButton: UIButton!
#IBOutlet weak var cancelButton: UIButton!
#IBOutlet weak var addItemButton: UIButton!
//Commonly Used Objects/Variables
var potentialBuyers: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
potentialBuyers.append("")
tableView.dataSource = self
tableView.register(UINib(nibName: "BuyerCell", bundle: nil), forCellReuseIdentifier: "ReusableCell")
}
override func viewWillAppear(_ animated: Bool) {
}
#IBAction func splitItemPressed(_ sender: UIButton) {
potentialBuyers.append("")
tableView.reloadData()
}
}
Here are the tableview datasource and the delete button delegate.
extension EntryAlertViewController: UITableViewDataSource, DeleteButtonDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return potentialBuyers.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ReusableCell", for: indexPath) as! BuyerCell
cell.deleteButtonDelegate = self
cell.indexPath = indexPath
cell.nameField.text = cell.buyerName
if potentialBuyers.count == 1 {
cell.deleteButton.isHidden = true
} else {
cell.deleteButton.isHidden = false
}
return cell
}
func deletePressed(index: Int) {
potentialBuyers.remove(at: index)
tableView.reloadData()
}
}
And here is my BuyerCell class with the UITextFieldDelegate as an extension.
import UIKit
protocol DeleteButtonDelegate {
func deletePressed(index: Int)
}
class BuyerCell: UITableViewCell {
#IBOutlet weak var deleteButton: UIButton!
#IBOutlet weak var nameField: UITextField!
var deleteButtonDelegate: DeleteButtonDelegate!
var indexPath: IndexPath!
var buyerName: String?
override func awakeFromNib() {
super.awakeFromNib()
self.nameField.delegate = self
}
#IBAction func deletePressed(_ sender: UIButton) {
//print the indexPath.row that this was pressed for
print("delet pressed for \(indexPath.row)")
self.deleteButtonDelegate?.deletePressed(index: indexPath.row)
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
extension BuyerCell: UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
print("textFieldDidBeginEditing")
buyerName = nameField.text
}
func textFieldDidEndEditing(_ textField: UITextField) {
print("textFieldDidEndEditing")
buyerName = nameField.text
}
}
Your problem is in this line
cell.nameField.text = cell.buyerName
Cells are reused from a reuse pool, so you can't rely on the cell holding any particular state or value.
Your buyer name needs to come from your data model array.
Something like
cell.nameField.text = self.potentialBuyers[indexPath.row]
Reloading the whole tableview is a bit excessive when you have only deleted a single row; Just delete the relevant row.
You can also clean up your delegation protocol so that there is no need for the cell to track its indexPath -
protocol DeleteButtonDelegate {
func deletePressed(in cell: UITableViewCell)
}
In your cell:
#IBAction func deletePressed(_ sender: UIButton) {
self.deleteButtonDelegate?.deletePressed(in: self)
}
In your view controller:
func deletePressed(in cell: UITableViewCell) {
guard let indexPath = tableView.indexPath(for: cell) else {
return
}
potentialBuyers.remove(at: indexPath.row)
tableView.deleteRows(at:[indexPath], with: .automatic)
}
There is a major issue in your code. You are not updating the data model so the changes in the cells are lost when the user scrolls.
Rather then quite objective-c-ish protocol/delegate in Swift callback closures are much more convenient and efficient. You can use one callback for both updating the model and deleting the cell.
Replace the BuyerCell cell with
class BuyerCell: UITableViewCell {
#IBOutlet weak var deleteButton: UIButton!
#IBOutlet weak var nameField: UITextField!
var callback : ((UITableViewCell, String?) -> Void)?
override func awakeFromNib() {
super.awakeFromNib()
self.nameField.delegate = self
}
#IBAction func deletePressed(_ sender: UIButton) {
callback?(self, nil)
}
}
extension BuyerCell: UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
print("textFieldDidBeginEditing")
callback?(self, nameField.text)
}
func textFieldDidEndEditing(_ textField: UITextField) {
print("textFieldDidEndEditing")
callback?(self, nameField.text)
}
}
In the controller in cellForRow assign the callback and handle the actions. The actions work also reliably if cells are reordered, inserted or deleted.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ReusableCell", for: indexPath) as! BuyerCell
let buyerName = potentialBuyers[indexPath.row]
cell.nameField.text = buyerName
cell.callback = { [unowned self] cCell, cName in
let currentIndexPath = tableView.indexPath(for: cCell)!
if let name = cName {
self.potentialBuyers[currentIndexPath.row] = name
} else {
self.potentialBuyers.remove(at: currentIndexPath.row)
tableView.deleteRows(at: [currentIndexPath], with: .fade)
}
}
cell.deleteButton.isHidden = potentialBuyers.count == 1
return cell
}
I tried tableview inside tableview, sub tableview contains drop down of 4 fields and if click on sub table view row then it will hide and shown in the dropdown button. pls check my below code.
import UIKit
var empname = ["Alex","Henry","Smith","Carey","Stephen"]
var insideArr = ["Retired", "Newly Joined", "Resigned", "Closed" ]
class EmpViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView.tag == 100{
return empname.count
}
else{
return insideArr.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView.tag == 100{
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! DataCell
cell.emp.text = empname[indexPath.item] as? String
cell.btndropdown.tag = indexPath.row
cell.btndropdown.addTarget(self, action: #selector(self.btndrop(sender:)), for: UIControl.Event.touchUpInside)
cell.btndropdown.setTitle("Open", for: .normal)
return cell
}
else{
let cell = tableView.dequeueReusableCell(withIdentifier: "InnerDataCell", for: indexPath) as! InnerDataCell
cell.role.text = insideArr[indexPath.item] as? String
return cell
}
}
#objc func btndrop(sender: UIButton!){
if(sender.isSelected == true){
sender.isSelected = false
}
else{
sender.isSelected = true
}
}
}
class InnerDataCell: UITableViewCell {
#IBOutlet weak var lblInsideName: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
class DataCell: UITableViewCell {
#IBOutlet weak var tblInsideTableView: UITableView!
#IBOutlet weak var sitename: UILabel!
#IBOutlet weak var btndropdown: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
extension DataCell{
func setTableViewDataSourceDelegate
<D:UITableViewDelegate & UITableViewDataSource>
(_ dataSourceDelegate: D, forRow row: Int){
tblInsideTableView.delegate = dataSourceDelegate
tblInsideTableView.dataSource = dataSourceDelegate
tblInsideTableView.reloadData()
}
}
Required output:
Emp1 select V
Emp2 select V
if click on Select button in 1st index, then
Emp1 Select V
Emp2 Select V
Retired
Newly Joined
Resigned
Closed
if click on closed option then it will be
Emp1 Select V
Emp2 Closed V
I am using BEMcheckbox. when i click it, it animates and show a hidden label but when I scroll my tableview my checkbox is automatically deselected. also when I scroll it doesn't select any checkbox automatically. what I want is when I scroll my tableview the checkbox which are checked remains checked and which are unchecked remains unchecked. my code is below. my view controller class.
class markAttendanceViewController: UIViewController , UITableViewDataSource , UITableViewDelegate{
#IBAction func selectall(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
if sender.isSelected {
checkImageView.isHidden = false
checkboxLabel.layer.borderColor = UIColor.blue.cgColor
} else{
checkImageView.isHidden = true
checkboxLabel.layer.borderColor = UIColor.lightGray.cgColor
}
table.reloadData()
}
#IBOutlet weak var checkImageView: UIImageView!
#IBOutlet weak var checkboxLabel: UILabel!
#IBAction func backToAttendanceView(_ sender: AnyObject) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var controller: UIViewController!
controller = storyboard.instantiateViewController(withIdentifier: "listViewController") as! listViewController
(controller as! listViewController).receivedString = "Mark Attendance"
let navController = UINavigationController(rootViewController: controller)
let revealController = self.revealViewController() as! RevealViewController
revealController.rightViewController = navController
revealController.rightViewController.view.addGestureRecognizer(revealController.panGestureRecognizer())
self.present(revealController, animated: true, completion: nil)
}
#IBOutlet weak var table: UITableView!
#IBOutlet weak var button: UIButton!
//var items:Array = [String]()
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.setNavigationBarHidden(true, animated: true)
checkboxLabel.layer.borderWidth = 1
checkboxLabel.layer.borderColor = UIColor.lightGray.cgColor
// items = ["Dashboard","Mark Attendance","Update Attendance","delete Attendance","Time Table","Academic Calendar","Reports","About Us","Logout","rbivwe","whefo","ewsow","webkgwo","wbiebfkwbei","ejwvabei","vdkgdvkJDB"]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 20
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "attendanceTableViewCell") as! attendanceTableViewCell
// cell.studentname?.text = items[indexPath.row]
cell.serialnumber?.text = "\(indexPath.row + 1)"
if button.isSelected {
cell.present.isHidden = false
cell.box.setOn(true, animated: true)
} else
{
cell.box.setOn(false, animated: false)
cell.present.isHidden = true
}
return cell
}
}
My tableview cell class.
class attendanceTableViewCell: UITableViewCell,BEMCheckBoxDelegate {
#IBOutlet weak var present: UILabel!
#IBOutlet weak var box: BEMCheckBox!
#IBOutlet weak var studentname: UILabel!
#IBOutlet weak var serialnumber: UILabel!
#IBOutlet weak var view: UIView!
override func awakeFromNib() {
box.delegate = self
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
view.layer.masksToBounds = false
view.layer.cornerRadius = 2.0
view.layer.shadowOffset = CGSize(width: -1, height: 1)
view.layer.shadowOpacity = 0.2
// Configure the view for the selected state
}
func didTap(_ checkBox: BEMCheckBox) {
if box.on {
present.isHidden = false
} else {
present.isHidden = true
}
}
}
If someone still need a solution for this. The only way I got it work is to add checkBox state each time you tap on it and then check the state in cellForRowAt function. My suggestion:
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, BEMCheckBoxDelegate {
//...
var checkboxesState: [Int: Bool] = [:]
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = mainTableView.dequeueReusableCell(withIdentifier: "productCell", for: indexPath) as! ProductCell
cell.checkBox.delegate = self
cell.checkBox.tag = indexPath.row
if let isOn = checkboxesState[indexPath.row] {
if isOn {
cell.checkBox.on = true
} else {
cell.checkBox.on = false
}
} else {
cell.checkBox.on = false
}
//... other code
return cell
}
func didTap(_ checkBox: BEMCheckBox) {
checkboxesState.updateValue(checkBox.on, forKey: checkBox.tag)
}
//...
}