Increase and Decrease values on Button click - ios

I want to show value on UIlabel. when I press UIbutton to increase or decrease values on label. This is my code and when I am running my project but I didn't get any value on my uilabel.
#IBAction func btnIncreaseAction(_ sender: Any) {
var count = 0
count = (count + 1)
if let cell = (sender as? UIButton)?.superview?.superview?.superview as? ShoppingCell
{
//cell.lblForOnty.text = "\(cell.lblForOnty.text ?? 0 + 1)"
cell.lblForOnty.text = String(count)
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellRID") as! ShoppingCell
var someValue: Int = 0 {
didSet {
cell.lblForOnty.text = "\(count)"
}
}
return cell
}
}

This is the code which should be in your ViewController. I am assuming that numberOfSections is 1 and in numberOfRowsInSection you are passing then number of rows you want. Else you need to modify this line : let indexPath = IndexPath(row: sender.tag, section: 0).
var count = 0 // Count variable should be a global variable, you need it to decrease the value too.
#objc func increaseCounter(sender: UIButton) {
//increase logic here
count = (count + 1)
let indexPath = IndexPath(row: sender.tag, section: 0)
let cell = tableView.cellForRow(at: indexPath)
cell.lblForOnty.text = "\(count)"
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellRID") as! ShoppingCell
// Add tag and action to your button
cell.yourButton.tag = indexPath.row
cell.yourButton.addTarget(self, action: #selector(increaseCounter(sender:)), for: .touchUpInside)
return cell
}

superview?.superview?.superview is pretty weird. Don't do that. A callback is more reliable.
In the subclass ShoppingCell create a callback and the IBActions for in- and decrease . Connect the actions to the buttons
class ShoppingCell: UITableViewCell {
#IBOutlet weak var lblForOnty: UILabel!
var callback : ((Int)->())?
var counter = 0 {
didSet {
lblForOnty.text = "\(count)"
}
}
#IBAction func btnIncreaseAction(_ sender: UIButton) {
counter += 1
callback?(counter)
}
#IBAction func btnDecreaseAction(_ sender: UIButton) {
if counter > 0 { counter -= 1 }
callback?(counter)
}
}
In cellForRow pass the counter value to the cell and use the callback to update the model which is represented by dataSource and which is a custom struct or class containing a property counter.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellRID", for: indexPath) as! ShoppingCell
let item = dataSource[indexPath.row]
cell.counter = item.counter
cell.callback = ( newValue in
item.counter = newValue
}
return cell
}
No hassle with superviews and index paths.

Move you count variable outside of the function
Increment/decrement the count inside the function and reload the table or you can reload particular index as well.
PFB the code snipped
let indexPath = NSIndexPath(forRow: rowNumber, inSection: 0)
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Top)

Please follows these step:
Step 1 :
create object class
class QuantityBO : NSObject{
var quantity : Int?
}
step 2:
Create global array in your class
var arrQuantityList = [QuantityBO]()
step 3 :
Assign the value according to your number of cell
It may be change according to your api resonse
step 4:
In cellForRowAt method please write:
cell.lblQuantity.text = String(arrQuantityList[indexPath.row].quantity)
cell.yourBtnName.tag = indexPath.row
step 5:
On your button action
arrQuantityList[sender.tag].quantity = arrQuantityList[sender.tag].quantity + 1
let indexPath = NSIndexPath(forRow: sender.tag, inSection: 0)
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.none)
It may helps to you.hank you.

Related

plus or minus button values is not updated in swift

I working on cart view controller I tried some of code, it's show like below image.
values updating every row in tableview, if I click on product1 plusbutton count is increase showing 1 .when I click on product2 plusbutton value is showing 2.count is increasing.minus every time minusbutton also working same like that.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell") as! CustomTableViewCell
cell.minusButton.tag = indexPath.row
cell.plusbutton.tag = indexPath.row
cell.minusButton.addTarget(self, action: #selector(minusbuttonClick), for: .touchUpInside)
cell.plusbutton.addTarget(self, action: #selector(plusButtonClick), for: .touchUpInside)
return cell
}
#objc func minusbuttonClick(sender : UIButton)
{
let cell = Tableview.cellForRow(at: NSIndexPath(row: sender.tag, section: 0) as IndexPath) as! CustomTableViewCell
if(count > 0){
count -= 1
}
let myString = String(count)
cell.countLabel.text = myString
if count == 0{
cell.countLabel.text = ""
}
self.Tableview.reloadData()
}
#objc func plusButtonClick(sender : UIButton)
{
let cell = Tableview.cellForRow(at: NSIndexPath(row: sender.tag, section: 0) as IndexPath) as! CustomTableViewCell
count += 1
let myString = String(count)
cell.countLabel.text = myString
self.Tableview.reloadData()
}
I have to show when I click product1 value should be 1, if I click on product2 value as 1
minus also decrease same like that
first thing to do is create a struct to hold your data
struct ProductData {
var count : Int
var name : String
}
and you can use that in your ViewController
var productData : [ProductData] = []
the, of course, you need to add some data - here's a simple set to get started
override func viewDidLoad() {
super.viewDidLoad()
// set up some data.
productData.append(ProductData(count: 0, name: "product 1"))
productData.append(ProductData(count: 0, name: "product 2"))
productData.append(ProductData(count: 0, name: "product 3"))
productData.append(ProductData(count: 0, name: "product 4"))
}
using the delegate model described earlier swift: how to get the indexpath.row when a button in a cell is tapped? update your custom table view cell
protocol TableViewCellCustomDelegate: class {
func buttonTapped(index : Int, delta : Int)
}
class TableViewCellCustom: UITableViewCell {
weak var delegate: TableViewCellCustomDelegate?
#IBOutlet weak var minusButton: UIButton!
#IBOutlet weak var plusButton: UIButton!
#IBOutlet weak var countLabel: UILabel!
#IBOutlet weak var productLabel: UILabel!
override func prepareForReuse() {
super.prepareForReuse()
self.delegate = nil
}
#IBAction func minusButtonClick(_ sender: UIButton) {
delegate?.buttonTapped(index: sender.tag, delta : -1)
}
#IBAction func plusButtonClick(_ sender: UIButton) {
delegate?.buttonTapped(index: sender.tag,delta : +1)
}
}
update the delegate methods in your view controller. I've done it here with a single method to add or delete, but you could split it out if you want to do other things as well.
extension ViewController: TableViewCellCustomDelegate {
func buttonTapped(index: Int, delta : Int)
{
productData[index].count += delta
if productData[index].count < 0
{
productData[index].count = 0
}
tableView.reloadRows(at: [IndexPath(row: index, section: 0)], with: .automatic)
}
}
and update your cellForRowAt definition
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as! TableViewCellCustom
cell.countLabel.text = "\(productData[indexPath.row].count)"
cell.productLabel.text = productData[indexPath.row].name
cell.minusButton.tag = indexPath.row
cell.plusButton.tag = indexPath.row
cell.delegate = self
return cell
}

Swift TableView insert row below button clicked

I am new to Swift and I am using Swift 4.2 . I have a TableView with a label and button . When I press a button I would like to add a new row directly below the row in which the button was clicked . Right now when I click a button the new row gets added to the bottom of the TableView every time. I have been looking at posts on here but haven't been able to get it working this is my code base . I have a method called RowClick I get the indexpath of the row that was clicked but do not know how to use that to get the new row to appear directly below the clicked row .
class ExpandController: UIViewController,UITableViewDelegate,UITableViewDataSource {
#IBOutlet weak var TableSource: UITableView!
var videos: [String] = ["FaceBook","Twitter","Instagram"]
override func viewDidLoad() {
super.viewDidLoad()
TableSource.delegate = self
TableSource.dataSource = self
TableSource.tableFooterView = UIView(frame: CGRect.zero)
// Do any additional setup after loading the view.
}
#IBAction func RowClick(_ sender: UIButton) {
guard let cell = sender.superview?.superview as? ExpandTVC else {
return
}
let indexPath = TableSource.indexPath(for: cell)
InsertVideoTitle(indexPath: indexPath)
}
func InsertVideoTitle(indexPath: IndexPath?)
{
videos.append("Snapchat")
let indexPath = IndexPath(row: videos.count - 1, section: 0)
TableSource.beginUpdates()
TableSource.insertRows(at: [indexPath], with: .automatic)
TableSource.endUpdates()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return videos.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let videoTitle = videos[indexPath.row]
let cell = TableSource.dequeueReusableCell(withIdentifier: "ExpandTVC") as! ExpandTVC
cell.Title.text = videoTitle
cell.ButtonRow.tag = indexPath.row
cell.ButtonRow.setTitle("Rows",for: .normal)
return cell
}
}
This is how my table looks I clicked the Facebook Rows button and it appended the string SnapChat . The Snapchat label should appear in a row below Facebook instead . Any suggestions would be great !
I think the easiest solution without re-writing this whole thing would be adding 1 to the current row of the IndexPath you captured from the action.
let indexPath = TableSource.indexPath(for: cell)
var newIndexPath = indexPath;
newIndexPath.row += 1;
InsertVideoTitle(indexPath: newIndexPath);
I did this from memory because I am not near an IDE, so take a look at the change and apply that change if needed in any other location.
class ExpandController: UIViewController,UITableViewDelegate,UITableViewDataSource {
#IBOutlet weak var TableSource: UITableView!
var videos: [String] = ["FaceBook","Twitter","Instagram"]
override func viewDidLoad() {
super.viewDidLoad()
TableSource.delegate = self
TableSource.dataSource = self
TableSource.tableFooterView = UIView(frame: CGRect.zero)
// Do any additional setup after loading the view.
}
#IBAction func RowClick(_ sender: UIButton) {
guard let cell = sender.superview?.superview as? ExpandTVC else {
return
}
let indexPath = TableSource.indexPath(for: cell)
var newIndexPath = indexPath;
newIndexPath.row += 1;
InsertVideoTitle(indexPath: newIndexPath);
}
func InsertVideoTitle(indexPath: IndexPath?)
{
videos.append("Snapchat")
let indexPath = IndexPath(row: videos.count - 1, section: 0)
TableSource.beginUpdates()
TableSource.insertRows(at: [indexPath], with: .automatic)
TableSource.endUpdates()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return videos.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let videoTitle = videos[indexPath.row]
let cell = TableSource.dequeueReusableCell(withIdentifier: "ExpandTVC") as! ExpandTVC
cell.Title.text = videoTitle
cell.ButtonRow.tag = indexPath.row
cell.ButtonRow.setTitle("Rows",for: .normal)
return cell
}
}
Your current code calls append to add the new item at the end of the array. What you want to do is insert a new row at indexPath.row+1. Array has an insert(element,at:) function.
You have to handle the case where the user has tapped the last row and not add 1 to avoid an array bounds error:
func InsertVideoTitle(indexPath: IndexPath)
{
let targetRow = indexPath.row < videos.endIndex ? indexPath.row+1 : indexPath.row
videos.insert("Snapchat" at:targetRow)
let newIndexPath = IndexPath(row: targetRow, section: 0)
TableSource.beginUpdates()
TableSource.insertRows(at: [newIndexPath], with: .automatic)
TableSource.endUpdates()
}

SegmentControl duplicating Checkmarks on UITableviewCell

Basically I am using the pod M13Checkbox which works great when I do not incorporate my UISegmentedControl. I have two todo lists separated into two segments. As soon as I check a box on the first segment it is repeated on the second segment.
I'm not sure if this is because of the cell being reused or not. Using print statements I have figured out I am able to see the data of the indexpath.row disregarding what segment is currently showing. Just the checkmarks I cannot control.
Please disregard any "Firebase notes".
class ToDoList: UITableViewController {
#IBOutlet weak var segment: UISegmentedControl!
var selectedRow: Int = 0
let seg1 = ["first","tab","Index"]
let seg2 = ["second","segment","akjdhfajd"]
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var returnValue = 0
switch(segment.selectedSegmentIndex) {
case 0: returnValue = seg1.count
case 1: returnValue = seg2.count
default: break
}
return returnValue
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ToDoCell", for: indexPath) as! ToDoCell
switch (segment.selectedSegmentIndex) {
case 0:
segment.selectedSegmentIndex = 0
selectedRow = 0
cell.item.text = seg1[indexPath.row]
cell.box.tag = indexPath.row
case 1:
segment.selectedSegmentIndex = 1
selectedRow = 1
cell.item.text = seg2[indexPath.row]
cell.box.tag = indexPath.row
print(cell.box.tag)
default:
break
}
cell.box.addTarget(self, action: Selector(("checkboxPressed")), for: UIControlEvents.touchUpInside)
return cell
}
#IBAction func checkboxPressed(_ sender: M13Checkbox) {
let buttonRow = sender.tag
let listItem = [buttonRow]
let todoDict = ["\(buttonRow)": "True"]
//need to individually checkbox the cases .. at moment if i check one it checks the other also
if sender.checkState == .checked || sender.checkedValue == nil {
print("\(buttonRow)")
if selectedRow == 0 {
let listArrayItem = seg1[buttonRow]
sender.checkedValue = listArrayItem
print(listArrayItem)
} else {
print("we are on segment 2")
}
} else if sender.checkState == .unchecked {
// if its unchecked. then u want to remove it from the checked item in firebase.. we will only load checked items obviosuly..
//remove from completed list
}
}
}

Increase/Decrease a value and display results in a Label inside a TableViewCell Swift Xcode

I have a ViewController with a TableView and a TableViewCell containing multiple sections and rows.
I have 2 button "plus" and "minus" and a label "totalLabel" in each row.
How can I get the value displayed in the label for each specific row when the user presses the + or - button?
for now when I run the app and press the + or - buttons only the totalLabel of the section 0/row 0 is working while random values just appear and disappear in the other sections/rows
my tableViewCell code :
import UIKit
protocol CommandeCellDelegate: class {
}
class CommandeCell: UITableViewCell {
weak var delegate : CommandeCellDelegate!
#IBOutlet weak var drinksLabel: UILabel!
#IBOutlet weak var priceLabel: UILabel!
#IBOutlet weak var totalLabel: UILabel!
#IBOutlet weak var plusButton: UIButton!
#IBOutlet weak var minusButton: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
here is my code for cellForRowAt :
class MenuViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, CommandeCellDelegate {
var count : Int = 0
var countValue : String!
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CommandeCell", for: indexPath) as! CommandeCell
cell.plusButton.tag = indexPath.section
cell.plusButton.tag = indexPath.row
cell.plusButton.addTarget(self, action: #selector(self.increaseValue), for: .touchUpInside)
cell.minusButton.tag = indexPath.section
cell.minusButton.tag = indexPath.row
cell.minusButton.addTarget(self, action: #selector(self.decreaseValue), for: .touchUpInside)
if indexPath.section == 0 {
let softInfo = softs[indexPath.row]
cell.drinksLabel?.text = softInfo.drinkName
cell.totalLabel?.text = // how to display countValue here?
let HappyHourStatus = partner!.barHHStatus
if case "0" = HappyHourStatus {
cell.priceLabel?.text = softInfo.drinkHHPrice
} else
if case "1" = HappyHourStatus {
cell.priceLabel?.text = softInfo.drinkPrice
}
}
else if indexPath.section == 1 {
let cocktailInfo = cocktails[indexPath.row]
cell.drinksLabel?.text = cocktailInfo.drinkName
cell.totalLabel?.text = // how to display countValue here?
let HappyHourStatus = partner!.barHHStatus
if case "0" = HappyHourStatus {
cell.priceLabel?.text = cocktailInfo.drinkHHPrice
} else
if case "1" = HappyHourStatus {
cell.priceLabel?.text = cocktailInfo.drinkPrice
}
}
return cell
}
and my funcs to increase or decrease the value :
func increaseValue(_ sender: UIButton) -> Int {
count = 1 + count
print(count)
countValue = "\(count)"
let rowToReload = IndexPath(row: sender.tag, section: sender.tag)
let rowsToReload: [Any] = [rowToReload]
tableView.reloadRows(at: rowsToReload as! [IndexPath], with: .automatic)
return count
}
func decreaseValue(_ sender: UIButton) -> Int {
if count == 0 {
print("Count zero")
} else {
count = count - 1
}
countValue = "\(count)"
let rowToReload = IndexPath(row: sender.tag, section: sender.tag)
let rowsToReload: [Any] = [rowToReload]
tableView.reloadRows(at: rowsToReload as! [IndexPath], with: .automatic)
return count
}
I have tried countless solutions but so far none is working - thank you for your help!
So your problem is this code
cell.plusButton.tag = indexPath.section
cell.plusButton.tag = indexPath.row
A tag can only store one value. So you are overriding the section with the row. So it is going to cause all sorts of weirdness. The better solution is to determine what cell you are targeting based on the button itself. Since you know what button was clicked you can convert the location of this button to a point on the table view. And then that point to a a particular index path.
So using your example code you can do something like below:
var softsCount: [Int] = []
var cocktailsCount: [Int] = []
override func viewDidLoad() {
super.viewDidLoad()
softsCount = Array(repeating: 0, count: softs.count) // Fill an array with 0
cocktailsCount = Array(repeating: 0, count: cocktails.count) // Fill an array with 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
...
if indexPath.section == 0 {
...
cell.totalLabel?.text = "\(softsCount[indexPath.row])"
...
} else if indexPath.section == 1 {
...
cell.totalLabel?.text = "\(cocktailsCount[indexPath.row])"
...
}
...
}
func increaseValue(_ sender: UIButton) {
let pointInTable = sender.convert(sender.bounds.origin, to: tableView)
if let indexPath = self.tableView.indexPathForRow(at: pointInTable), let cell = tableView.cellForRow(at: indexPath) {
if indexPath.section == 0 {
softsCount[indexPath.row] += 1
cell.totalLabel?.text = "\(softsCount[indexPath.row])"
} else if indexPath.section == 1 {
cocktailsCount[indexPath.row] += 1
cell.totalLabel?.text = "\(cocktailsCount[indexPath.row])"
}
}
}
No sure why you are returning count. I am sure this is just a partial implementation. But the button should take care of the entire action including updating the label with the new count. You don't normally return values from button presses.
So updated the example to update the label with the current count. Since I am unable to see what your drinks object I made an assumption that the drinks class has a count parameter that starts at 0. This way each individual drink has a count assigned to it.

cellForRow(at: indexPath) returns nil Swift3

I have UITableView with a custom cell IconsTableViewCell holding one UIImageView and one UILable.
If a row was previously selected, when user taps on a new row, the previous row is deselected and the label's textcolor for the new row should change.
However, when I try to get a reference to the current cell using indexPath, the app crashes. I am stuck at this for the past few hours.
class EighthViewController: UIViewController, UITableViewDelegate,UITableViewDataSource {
let checkedImage = UIImage(named: "checked")!
let uncheckedImage = UIImage(named: "unchecked")!
struct Item {
var name:String // name of the rows
var selected:Bool // whether is selected or not
var amount: Int // value of the items
}
var frequency = [
Item(name:"Every week",selected: false, amount: 0),
Item(name:"Every 2 weeks",selected: false, amount: 0),
Item(name:"Every 4 weeks",selected: false, amount: 0),
Item(name:"Once",selected: false, amount: 0),
Item(name:"End of tenancy cleaning", selected: false, amount: 0)
]
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// retrieve indexPathForCellSelected from UserDefaults
if let retrievedIndexPath = UserDefaults.standard.data(forKey: indexKey) {
if let data1 = NSKeyedUnarchiver.unarchiveObject(with: retrievedIndexPath) as? IndexPath {
indexPathForCellSelected = data1
/* Inform the delegate that the row has already been selected.
When calling 'tableView:didSelectRowAtIndexPath:', it will calculate the total amount depending on the type of cleaning:
Weekly, End of Tenancy..etc
Call calculateTotal() function which is using `indexPathForCellSelected`to calculate the total */
self.tableView(self.tableView, didSelectRowAt: indexPathForCellSelected!)
// assign the indexPath retrieved to StructS.indexPath
StructS.indexPath = indexPathForCellSelected
// assign StructS.price to self.frequencyTotalPrice
self.frequencyTotalPrice = StructS.price
// assign self.frequencyTotalPrice to FullData.finalFrequecyAmount
FullData.finalFrequecyAmount = self.frequencyTotalPrice
// assign a Checkmark to the row with the corresponding indexPathForCellSelected retrieved
tableView.cellForRow(at: indexPathForCellSelected!)?.accessoryType = .checkmark
tableView.cellForRow(at: indexPathForCellSelected!)?.imageView?.image = checkedImage
let cell = tableView.cellForRow(at: indexPathForCellSelected!) as! IconsTableViewCell
cell.frequencyLabel.textColor = .black
// assign frequency[indexPath.row].name to FullData structure
FullData.finalFrequencyName = frequency[indexPathForCellSelected!.row].name
//assign the row as Int value to a global var so as to determine which ViewController to unwind segue in 10th ViewController
StructS.frequencyRowSelectedEighthVC = indexPathForCellSelected!.row
}
}
// handle the selection of the row so as to update the values of labels in section header.
// if indexPathForCellSelected == nil, select a default type of cleaning for the first time
if indexPathForCellSelected == nil {
// construct an indexPath for the row we want to select when no previous row was selected ( not already saved in UserDefaults)
let rowToSelect:IndexPath = IndexPath(row: 1, section: 0)
// select the row at `rowToSelect` indexPath. This will just register the selectd row, However,the code that you have in tableView:didSelectRowAtIndexPath: is not yet executed because the delegate for the tablewView object in the ViewController has not been called yet.
self.tableView.selectRow(at: rowToSelect, animated: true, scrollPosition: UITableViewScrollPosition.none)
// inform the delegate that the row was selected
// stackoverflow.com/questions/24787098/programmatically-emulate-the-selection-in-uitableviewcontroller-in-swift
self.tableView(self.tableView, didSelectRowAt: rowToSelect)
//assign the row as Int value to a global var so as to determine which ViewController to unwind segue in 10th ViewController
StructS.frequencyRowSelectedEighthVC = rowToSelect.row
print("the row that was selected is\(StructS.frequencyRowSelectedEighthVC) ")
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return frequency.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
// configure the cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
-> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! IconsTableViewCell
cell.frequencyLabel.text = frequency[indexPath.row].name
cell.frequencyLabel.textColor = .gray
cell.iconImageView.image = uncheckedImage
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if !frequency[indexPath.row].selected {
// this avoid set initial value for the first time
if let index = indexPathForCellSelected {
// clear the previous cell
frequency[index.row].selected = false
tableView.cellForRow(at: index)?.accessoryType = .none
tableView.cellForRow(at: index)?.imageView?.image = nil
}
//mark the new row
frequency[indexPath.row].selected = true
tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
//assign checked image to row
tableView.cellForRow(at: indexPath)?.imageView?.image = checkedImage
//evaluates to nil when trying to get a reference to the cell at the selected indexPath
let cell = tableView.cellForRow(at: indexPath) as! IconsTableViewCell
cell.frequencyLabel.textColor = .black
//save indexPathForCellSelected in UserDefaults
if indexPathForCellSelected != nil {
// used to check if there is a selected row in the table
let data = NSKeyedArchiver.archivedData(withRootObject: indexPathForCellSelected!)
UserDefaults.standard.set(data, forKey: indexKey)
self.tableView.reloadData()
} // end of if indexPathForCellSelected
}
}
} //end of class
Create a property selectedRow. By default the first row is selected.
var selectedRow = 0
In viewWillAppear read the selected row from UserDefaults and reload the table view
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
selectedRow = UserDefaults.standard.integer(forKey: indexKey)
frequency[selectedRow].selected = true
tableView.reloadData()
}
In cellForRow set color, image and accessory view depending on the selected property
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell" for: indexPath) as! IconsTableViewCell
let freq = frequency[indexPath.row]
if freq.selected {
cell.accessoryType = .checkmark
cell.imageView?.image = checkedImage
cell.frequencyLabel.textColor = .gray
} else {
cell.accessoryType = .none
cell.imageView?.image = uncheckedImage
cell.frequencyLabel.textColor = .black
}
cell.frequencyLabel.text = freq.name
return cell
}
In didSelectRowAt compare the actual index path with selectedRow. If they are not equal set the selected property of the former selected cell to false and of the new selected cell to true. Then set selectedRow to the row of the index path, save the row to UserDefaults and reload the table view.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row != selectedRow {
let previousIndexPath = IndexPath(row:selectedRow, section:0)
frequency[selectedRow].selected = false
frequency[indexPath.row].selected = true
selectedRow = indexPath.row
UserDefaults.standard.set(selectedRow, forKey: indexKey)
tableView.reloadRows(at: [indexPath, previousIndexPath], with: .none)
}
}
I would try this (assuming you have an array of Item structs):
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
let item = myItemArray[indexPath.row]
(cell as! IconsTableViewCell).frequencyLabel.textColor = // get the color from your item selection state here
... // do other configurations to your cell
}

Resources