UISwitch in tableview row - ios

In the code below I'm populating my table with some data. The switches are off which they don't have to be. In the storyboard I defined it as On.
Cell:
var switchHandler: ((Bool)->Void)?
#IBAction func switchChanged(_ sender: UISwitch) {
self.switchHandler?(sender.isOn)
}
View controller:
var selectedCells = Set<IndexPath>()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SmsCell") as? SmsTableViewCell
cell?.PhonNumberLbl.text = data![indexPath.section].contacts[indexPath.row]?.phoneNumber
cell?.NameLbl.text = data![indexPath.section].contacts[indexPath.row]?.name
cell?.selectedTF.isOn = (data![indexPath.section].contacts[indexPath.row]?.selected)!
cell?.selectedTF.isOn = self.selectedCells.contains(indexPath)
cell?.switchHandler = { (switchState) in
if switchState {
self.selectedCells.insert(indexPath)
} else {
self.selectedCells.remove(indexPath)
}
}
return cell!
}
Model:
typealias smsModelList = [SmsModel]
struct SmsModel:Codable {
var unitNo:Int?
var unitPlaque:String?
var billText:String?
var contacts:[ContactsModel?]
}
typealias contactlistmodel = [ContactsModel]
struct ContactsModel:Codable
{
var id :Int?
var selected :Bool?
var phoneNumber : String?
var name : String?
}
Does anybody see somthing wrong which turns off the switch?

First of all as you force unwrap the cell anyway do it in the dequeue line to avoid the unnecessary amount of question marks and use the API to return a non-optional cell
let cell = tableView.dequeueReusableCell(withIdentifier: "SmsCell", for: indexPath) as! SmsTableViewCell
To fix your issue update the selected property of the ContactsModel struct directly and forget the extra selectedCells array. Further declare – at least – selected as non-optional, practically there is no maybe state. And declare also all data source arrays (data / contacts) as non-optional, cellForRow is called only if there is an item at the particular indexPath by default.
struct ContactsModel : Codable {
...
var selected : Bool
...
}
...
let cell = tableView.dequeueReusableCell(withIdentifier: "SmsCell", for: IndexPath) as! SmsTableViewCell
let contact = data[indexPath.section].contacts[indexPath.row]
cell.PhonNumberLbl.text = contact.phoneNumber
cell.NameLbl.text = contact.name
cell.selectedTF.isOn = contact.selected
cell.switchHandler = { [unowned self] switchState in
// as the structs are value types you have to specify the full reference to the data source array
self.data[indexPath.section].contacts[indexPath.row].selected = switchState
}
Consider to use classes rather than structs in this case then you can shorten the closure
cell.switchHandler = { switchState in
contact.selected = switchState
}

You use both
cell?.selectedTF.isOn = (data![indexPath.section].contacts[indexPath.row]?.selected)!
cell?.selectedTF.isOn = self.selectedCells.contains(indexPath)
so isOn property of the switch is controlled from 2 sides , so you have to decide which line that should be commnented , plus don't depend on storyboard prototype cell setup as because of cell reusing it' ll be changed , if you want to make them all on by default then change the var selectedCells to contain all possible indexPaths and comment the other one

Related

Obtain a data from a variable inside a struct in a different class Swift

I'm trying to obtain data from variables inside this structure for my UITableView cells
struct Issue {
var id: String
var tester: String
var type: issueType
var title: String
var appName: String
var desc: String
var date: Date
static func type(_ item: issueType) -> String {
if item == .major {
return "major"
}
else if item == .blocker {
return "blocker"
}
else if item == .minor {
return "minor"
}
return ""
}
}
But every time i try to get the data for my label, it didn't show up. Here's the code from my viewController
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)
let item = array[indexPath.row]
let topLine = cell.viewWithTag(1)
let labelStatus = cell.viewWithTag(2) as! UILabel
let labelTester = cell.viewWithTag(3) as! UILabel
let labelTitle = cell.viewWithTag(4) as! UILabel
let labelAppName = cell.viewWithTag(5) as! UILabel
let labelDesc = cell.viewWithTag(6) as! UILabel
let labelDate = cell.viewWithTag(7) as! UILabel
let labelId = cell.viewWithTag(8) as! UILabel
labelStatus.text = Issue.type(item.type).capitalized
labelTester.text = Issue.tester
return cell
}
P.S. The labelStatus text works fine
Setting text of labelStatus works, because you're calling static function on type Issue and this function returns String. In the second case you're trying to get property of type of struct, not of your certain item.
Use item's property
labelTester.text = item.tester

Passing XML Array Data to CollectionView via TableView

I'm attempting to pass an array of data from the view controller to the collection view cells. My collectionview is currently in a tableview. I have tried using delegation/protocols and creating arrays in the class and have not been able to successfully pass the data to my collectionview.
My code is a follows:
View Controller:
var ageUnder10: [MissingPerson] = []
var age10Plus: [MissingPerson] = []
var age15Plus: [MissingPerson] = []
if let ageRange = ageRange {
switch ageRange {
case .ageUnder10:
let ageUnder10Array = MissingPerson()
ageUnder10Array.title = self.missingPerson.title
ageUnder10Array.desc = self.missingPerson.desc
ageUnder10Array.url = self.missingPerson.url
self.ageUnder10.append(ageUnder10Array)
case .age10Plus:
let age10PlusArray = MissingPerson()
age10PlusArray.title = self.missingPerson.title
age10PlusArray.desc = self.missingPerson.desc
age10PlusArray.url = self.missingPerson.url
self.age10Plus.append(age10PlusArray)
case .age15Plus:
let age15PlusArray = MissingPerson()
age15PlusArray.title = self.missingPerson.title
age15PlusArray.desc = self.missingPerson.desc
age15PlusArray.url = self.missingPerson.url
self.age15Plus.append(age15PlusArray)
}
} else {
print("No valid age found")
}
Tableview Cell:
class TableViewCell: UITableViewCell {
var ageUnder10 = [MissingPerson]()
var age10Plus = [MissingPerson]()
var age15Plus = [MissingPerson]()
}
These values are being populated from an XML url
The categories are being created via scanner, scanning the values of a item in the xml (to create ageRange)
I have titleforheader and header names populated from a separate array in the view controller class
I figured it out, I needed to use a struct to pass the data. Also, create an instance of the array in the tableview class and write a function to fill the collectionView cell.
Example:
CustomTableViewCell:
customArray: [CustomArray]()
func configureCollectionCell(with array: [CustomArray]) {
self.customArray = customArray
}
ViewController Class:
var customArray = [CustomArray]()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as? CustomTableViewCell {
cell.configureCollectionCell(with: customArray)
return cell
}

Multiple sections in UITableView and UIButtons swift

I have a UITableView that has sections (Category0, Category1,..), and every row of a specific section is a UITableView that has one section which is the question (Question1,..) and rows which are the options to be answered (option1, option2,..).
The problem is when I click on a button in a specific category and a specific question (Category0, question1, option0) see screenshot1,
immediately another buttons in another categories are clicked (Category1, question2, option0) see screenshot2,
and (Category4, question1, option0) see screenshot3.
the code below:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? insideTableViewCell
cell?.answerlabel.text = "option \(indexPath.row)"
cell?.initCellItem(id: (myObject?.id)! , answer: (myObject?.answerArray![indexPath.row] as? String)!)
return cell!
}
In a custom UITableViewCell which is insideTableViewCell:
func initCellItem(id: Int , answer: String) {
radioButton.setImage( imageLiteral(resourceName: "unchecked"), for: .normal)
radioButton.setImage( imageLiteral(resourceName: "checked"), for: .selected)
radioButton.tag = id
radioButton.setTitle(answer, for: UIControlState.disabled)
radioButton.addTarget(self, action: #selector(self.radioButtonTapped), for: .touchUpInside)
}
#objc func radioButtonTapped(_ radioButton: UIButton) {
print(radioButton.tag)
print(radioButton.title(for: UIControlState.disabled) as Any)
let answer = radioButton.title(for: UIControlState.disabled) as Any
let StrId = String(radioButton.tag)
defaults.set(answer, forKey: StrId)
let isSelected = !self.radioButton.isSelected
self.radioButton.isSelected = isSelected
if isSelected {
deselectOtherButton()
}
}
func deselectOtherButton() {
let tableView = self.superview as! UITableView
let tappedCellIndexPath = tableView.indexPath(for: self)!
let section = tappedCellIndexPath.section
let rowCounts = tableView.numberOfRows(inSection: section)
for row in 0..<rowCounts {
if row != tappedCellIndexPath.row {
let cell = tableView.cellForRow(at: IndexPath(row: row, section: section)) as! insideTableViewCell
cell.radioButton.isSelected = false
}
}
}
You haven't posted code still guessing.
You can create model object like
class QuestionData {
var strQuestion:String? // This may contains Question
var strOptions:[String]? // It may contains options titles of your buttons
var selectedAnswerIndex:Int? // When any button tapped
}
And you should create category models like
class Categories {
var categoryTitle:String?
var questions:[QuestionData] = []
}
you can use this Categories class as main source of your dataSource array
var arrayDataSource = [Categories]()
And fill this with your original data.
now whenever any button tapped you can use selectedAnswerIndex:Int to store current selected option for question. and if it is null then user has not selected any option yet.
I have created class so it is reference type you can directly set the value without worry
Hope it is helpful to you
There has some and simple code I think it will help you :- if it is not sutable for you pls don't mind :-
if (!btnGreen3.isSelected)
{
btnGreen3.isSelected = !btnGreen3.isSelected
}
btnBlue3.isSelected = false
btnBlack3.isSelected = false
You need to save the states of every cell.
The reason is you are using dequereuseable cell with identifier when you scroll it switch to another cell.
So make Array or Dictionary where save the state of every selected and unselected Rows.

Swift global Array with table view

I am trying to create an empty global string arrray
struct GlobalVariables {
static var globalString = [String]()
}
I would like to initialize it like this by putting string values into the array
func percentageCalculation() -> String {
var FinalString = String()
for i in 1...100 {
let tstring = String("\(i)%\n")
GlobalVariables.globalString[i] = tstring
}
return FinalString
}
Then outputing global string values to my tableview.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
let testLabel = cell?.viewWithTag(1) as! UITextView
testLabel.text = GlobalVariables.globalString[indexPath.row]
return cell!
}
any advice on the best way to achieve this? This is my psuedo code of what im trying to accomplish
You don't need a struct to define a simple array. You can just define an array at a global level like this:
var globalString = [String]()
Once you have the array you can add your numbers to it like this:
for i in 1...100 {
globalString.append("\(i)")
}
Then in your cellForRow you can just use this to populate the values:
testLabel.text = globalString[indexPath.row]

Swift: Set Variables for TableViewCell

I have a tableView and my tableViewCell has a isLiked variable and a likeButton. I would like to set my likeButton's tintColor accordingly to the state of isLiked. The way I have done it is that I retrieve a list of posts from Firebase that the user has liked, and then pass it into the tableView. If the postID tallies with the list, I would like to set the isLiked variable to true.
However, despite setting this logic in cellForRow, the isLiked variable in tableViewCell is not set accordingly. How can I get it set accordingly? My code as follows:
// in TableViewController
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
let post: FIRDataSnapshot = self.posts[indexPath.row]
let postValues = post.value as! [String: AnyObject]
let postID = postValues["postID"] as! String
for likedPosts in usersLikedPostsList {
if likedPosts == postID {
DispatchQueue.main.async {
cell.isLiked = true
}
} else {
cell.isLiked = false
}
}
return cell
}
// in TableViewCell
class TableViewCell: UITableViewCell {
var isLiked: Bool?
#IBOutlet weak var likeButton: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
print(isLiked)
if let _ = isLiked {
likeButton.tintColor = .red
} else {
likeButton.tintColor = .darkGray
}
}
}
From the print statement, isLiked variable is nil when the table is loaded. I have attempted with self.tableView.reloadData() but it does not set it accordingly as well.
Note that I would like to manipulate isLiked state in TableViewCell because I wanna add code to toggle its state within TableViewCell, so that users can like and unlike the post, and perform the updates to Firebase accordingly as well.
The cell awakeFromNib() method is executed only once, when the first time the cell is instanciated from storyboard or Nib file, so you need to update your cell every time this value isLiked change, try with his code
// in TableViewCell
class TableViewCell: UITableViewCell {
var isLiked: Bool = false{
didSet{
likeButton.tintColor = .darkGray
if(isLiked)
{
likeButton.tintColor = .red
}
}
}
#IBOutlet weak var likeButton: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
}
}
Hope this helps you
let post: FIRDataSnapshot = self.posts[indexPath.row]
let postValues = post.value as! [String: AnyObject]
let postID = postValues["postID"] as! String
Try your above code in view did load and make postID as global variable.
and print to see if it returns anything or use .count to check it.
If it has count more than 1.Then try to use postID in method :cellForRowAt
Secondly, tableview reload will work only if numberOfRowsInSection have different value from last time.
Check whether this method is called or not when you reload table.

Resources