Deleting row by swipe not working propery in tableview - ios

I built an app using mvvm architecture. I don't know why
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath)
it's not getting called on my view controller (basically swipe doesn't work, it can work like 1/20). If I build the tableview (basic one) with its methods to another view controller it works well. I tried to build one more time from scratch my view controller, but also it doesn't affect the result, so the problem is definitely in the functionality of the controller.
My code where swipe works well (I did that only to check if that works in my app:)
class tbViewController: UIViewController {
#IBOutlet weak var tableview: UITableView!
private var data = ["1","2","3","4"]
override func viewDidLoad() {
super.viewDidLoad()
tableview.delegate = self
tableview.dataSource = self
}
}
extension tbViewController: UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = self.data[indexPath.row]
return cell
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
return .delete
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
tableView.beginUpdates()
self.data.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
tableView.endUpdates()
}
}
}
And my original view controller where swipe doesn't work, I also swapped original tableview with above one, only to verify if the problem is from tableview... I can't get it :C. (Please check first tableview methods) :
class ExpensesViewController: UIViewController, ChartViewDelegate {
#IBOutlet private weak var thisWeek: UIButton!
#IBOutlet private weak var thisMonth: UIButton!
#IBOutlet private weak var thisYear: UIButton!
#IBOutlet private weak var totalExpensedAmount: UILabel!
#IBOutlet private weak var pieChart: PieChartView!
#IBOutlet private weak var tableView: UITableView!
private var menu: SideMenuNavigationController?
private var tableViewActions = [Action]()
private var expensesViewModel = ExpensesViewModel()
private var data = ["1","2","3","4"]
override func viewDidLoad() {
super.viewDidLoad()
initSideBar()
customizeButtons()
customizePieChart()
self.tableView.delegate = self
self.tableView.dataSource = self
}
#IBAction func didTapMenu(){
present(self.menu!, animated: true)
}
#IBAction func onClickThisWeek(_ sender: UIButton) {
setGrayBackgroundAndBlackTitleColor(for: self.thisMonth, and: self.thisYear)
sender.backgroundColor = #colorLiteral(red: 0.05509413034, green: 0.7074701786, blue: 0.4755263329, alpha: 1)
sender.setTitleColor(#colorLiteral(red: 1, green: 1, blue: 1, alpha: 1), for: UIControl.State.normal)
}
#IBAction func onClickThisMonth(_ sender: UIButton) {
setGrayBackgroundAndBlackTitleColor(for: self.thisWeek, and: self.thisYear)
sender.backgroundColor = #colorLiteral(red: 0.05509413034, green: 0.7074701786, blue: 0.4755263329, alpha: 1)
sender.setTitleColor(#colorLiteral(red: 1, green: 1, blue: 1, alpha: 1), for: UIControl.State.normal)
}
#IBAction func onClickThisYear(_ sender: UIButton) {
setGrayBackgroundAndBlackTitleColor(for: self.thisWeek, and: self.thisMonth)
sender.backgroundColor = #colorLiteral(red: 0.05509413034, green: 0.7074701786, blue: 0.4755263329, alpha: 1)
sender.setTitleColor(#colorLiteral(red: 1, green: 1, blue: 1, alpha: 1), for: UIControl.State.normal)
}
private func setGrayBackgroundAndBlackTitleColor(for button1: UIButton, and button2: UIButton){
button1.backgroundColor = #colorLiteral(red: 0.921431005, green: 0.9214526415, blue: 0.9214410186, alpha: 1)
button2.backgroundColor = #colorLiteral(red: 0.921431005, green: 0.9214526415, blue: 0.9214410186, alpha: 1)
button1.setTitleColor(#colorLiteral(red: 0, green: 0, blue: 0, alpha: 1), for: UIControl.State.normal)
button2.setTitleColor(#colorLiteral(red: 0, green: 0, blue: 0, alpha: 1), for: UIControl.State.normal)
}
private func initSideBar(){
self.menu = SideMenuNavigationController(rootViewController: MenuListController())
self.menu?.leftSide = true
self.menu?.setNavigationBarHidden(true, animated: false)
SideMenuManager.default.leftMenuNavigationController = self.menu
SideMenuManager.default.addPanGestureToPresent(toView: self.view)
}
private func customizeButtons(){
self.thisWeek.setTitleColor(#colorLiteral(red: 1, green: 1, blue: 1, alpha: 1), for: UIControl.State.normal)
self.thisWeek.backgroundColor = #colorLiteral(red: 0.05509413034, green: 0.7074701786, blue: 0.4755263329, alpha: 1)
self.thisWeek.layer.borderColor = #colorLiteral(red: 0.05509413034, green: 0.7074701786, blue: 0.4755263329, alpha: 1)
self.thisMonth.layer.borderColor = #colorLiteral(red: 0.05509413034, green: 0.7074701786, blue: 0.4755263329, alpha: 1)
self.thisYear.layer.borderColor = #colorLiteral(red: 0.05509413034, green: 0.7074701786, blue: 0.4755263329, alpha: 1)
}
private func customizePieChart(){
self.pieChart.delegate = self
self.pieChart.noDataText = "You don't have any income, to display quarters income."
self.pieChart.legend.enabled = false
let pieChartAttribute = [ NSAttributedString.Key.font: UIFont(name: "Arial", size: 16.0)!, NSAttributedString.Key.foregroundColor: UIColor.init(displayP3Red: 0.462, green: 0.838, blue: 1.000, alpha: 1) ]
let pieChartAttrString = NSAttributedString(string: "Quarterly Revenue", attributes: pieChartAttribute)
self.pieChart.centerAttributedText = pieChartAttrString
}
}
extension ExpensesViewController: UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = self.data[indexPath.row]
return cell
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
return .delete
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
tableView.beginUpdates()
self.data.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
tableView.endUpdates()
}
}
}
How original screen should be:
How my screen is now, I commended basically all functionality to not confuse you, where my all business logic is, that's why nothing is displayed, it's just some UI changing, view model doesn't interact with controller(as you see in my code above):
If you have encountered such errors, please help me. Thanks a lot.

I would try using this: https://developer.apple.com/documentation/uikit/uitableviewdelegate/2902367-tableview
There’s also a similar function for leading swipes.

"I don't know why
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) it's not getting called on my view controller".
The reason your method it is not getting called is that you forgot to add at the declaration of your view controller that it should be the UITableViewDelegate.
Note: using beginUpdates method is pointless to delete/remove a single row. Btw when doing multiple insertions/deletions better to use performBatchUpdastes. Last but not least, it is Swift naming convention to name your classes starting with an uppercase letter.

Related

Selecting cell in 1 Collection View to change second Collection View properties (Xcode)

I’ve set up 2 Collection Views in Xcode - one holds empty cells with background colours and the other holds a collection of images.
When I select a cell in the first collection view I want to change all the background colours in the second Collection View to match the background colour of the cell I’ve selected.
The problem I’ve got is that only the cell with the matching index path changes in the second Collection View ie if I select the 3rd colour, the 3rd image changes.
How do I make it so that all the cells in the second Collection View change colour? I know it’s something to do with the index path but can’t figure out how to get round it.
Code:
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
#IBOutlet weak var colourView: UICollectionView!
#IBOutlet weak var iconView: UICollectionView!
#IBOutlet weak var height: NSLayoutConstraint!
let colours = [
UIColor(red: 0.94, green: 0.31, blue: 0.41, alpha: 1.00),
UIColor(red: 0.95, green: 0.45, blue: 0.23, alpha: 1.00),
UIColor(red: 1.00, green: 0.89, blue: 0.49, alpha: 1.00),
UIColor(red: 0.73, green: 0.72, blue: 0.42, alpha: 1.00),
UIColor(red: 0.01, green: 0.44, blue: 0.61, alpha: 1.00),
UIColor(red: 0.56, green: 0.89, blue: 0.90, alpha: 1.00),
UIColor(red: 0.84, green: 0.84, blue: 0.84, alpha: 1.00),
]
let images = [UIImage(named: "home"), UIImage(named: "work"), UIImage(named: "key"), UIImage(named: "supermarket"), UIImage(named: "restaurant"), UIImage(named: "tree") , UIImage(named: "parking"), UIImage(named: "rugby"), UIImage(named: "medicine"), UIImage(named: "mortar"), UIImage(named: "ticket"), UIImage(named: "star")]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if (collectionView == iconView){
return self.images.count
}
return self.colours.count
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = view.frame.size.width
if (collectionView == iconView){
return CGSize(width: width * 0.2, height: width * 0.2)
}
return CGSize(width: width * 0.1, height: width * 0.1)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if (collectionView == iconView){
let cell2 = iconView.dequeueReusableCell(withReuseIdentifier: "iconcell", for: indexPath) as! IconCollectionViewCell
cell2.iconimage.image = self.images[indexPath.item]
return cell2
}
let colourcell = colourView.dequeueReusableCell(withReuseIdentifier: "colourcell", for: indexPath) as! ColourCollectionViewCell
colourcell.backgroundColor = colours[indexPath.row]
return colourcell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
let cell = colourView.cellForItem(at: indexPath) as! ColourCollectionViewCell
let cell2 = iconView.cellForItem(at: indexPath) as! IconCollectionViewCell
if cell.isSelected {
cell2.iconimage.backgroundColor = cell.backgroundColor
}
}
}
Thanks in advance
Declare a var:
class ViewController:
var newColor : UIColor?
Change this:
if cell.isSelected {
cell2.iconimage.backgroundColor = cell.backgroundColor
}
for this:
if cell.isSelected {
newColor = cell.backgroundColor
iconView.reloadData()
}
in
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
put this
if newColor != nil { // maybe a better test here, if cell was selected
cell2.iconimage.backgroundColor = newColor
}
before
return cell2

Change height of specific dynamic prototype cell

I have a dynamic prototype table view. This table view displays an array of structs. Each struct is being represented by two cells, to be exact by DateCell and timelineCell.
In this case the date cell is the header and added with: func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {.
I want the DateCell (HeaderCell) on the top of the table view (first index of array) to have a different look. I've figured out how to change the background etc. (see viewForHeaderInSection) of this header, but how can I change the height of this exact cell?
extension TimelineViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func numberOfSections(in tableView: UITableView) -> Int {
return addDataArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let rowData = addDataArray[indexPath.section]
let cell = tableView.dequeueReusableCell(withIdentifier: "TimelineCell") as! TimelineCell
cell.setDrivenKm(drivenKm: rowData.driven)
cell.setConsumedL(consumedL: rowData.consumedL)
cell.setPricePerLiter(pricePerLiter: rowData.pricePerLiter)
return cell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let rowData = addDataArray[section]
guard let last = addDataArray.first else { return nil }
let color: UIColor!
let headerCell = tableView.dequeueReusableCell(withIdentifier: "DateCell") as! DateCell
if rowData.date == last.date {
color = UIColor.white
headerCell.backgroundColor = UIColor(red: 0/255, green: 22/255, blue: 60/255, alpha: 1)
} else {
color = UIColor(red: 0/255, green: 22/255, blue: 60/255, alpha: 1)
}
headerCell.setDate(date: rowData.date, color: color)
return headerCell
}
}
You need to implement ( for static )
func tableView(_ tableView: UITableView,
heightForHeaderInSection section: Int) -> CGFloat {
return 100.0
}
for dynamic
self.tableView.estimatedSectionHeaderHeight = 100.0
self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension
and make sure your constraints are hooked propely inside the cell , top , leading , trailing and bottom
Although the previous answer is correct, if you have only two items in the section (the header and the cell) and in your implementation both items are subclasses of UITableViewCell, then you can use both of them inside the cellForRowAt method. Just increase the number in numberOfRowsInSection to 2 and switch over indexPath.row.
extension TimelineViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
func numberOfSections(in tableView: UITableView) -> Int {
return addDataArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.row {
case 0:
let rowData = addDataArray[section]
guard let last = addDataArray.first else { return UITableViewCell() }
let color: UIColor!
let headerCell = tableView.dequeueReusableCell(withIdentifier: "DateCell", for: indexPath) as! DateCell
if rowData.date == last.date {
color = UIColor.white
headerCell.backgroundColor = UIColor(red: 0/255, green: 22/255, blue: 60/255, alpha: 1)
} else {
color = UIColor(red: 0/255, green: 22/255, blue: 60/255, alpha: 1)
}
headerCell.setDate(date: rowData.date, color: color)
return headerCell
case 1:
let rowData = addDataArray[indexPath.section]
let cell = tableView.dequeueReusableCell(withIdentifier: "TimelineCell", for: indexPath) as! TimelineCell
cell.setDrivenKm(drivenKm: rowData.driven)
cell.setConsumedL(consumedL: rowData.consumedL)
cell.setPricePerLiter(pricePerLiter: rowData.pricePerLiter)
return cell
default:
return UITableViewCell()
}
}
}
But if you really want to use a section header, I would use subclass of UITableViewHeaderFooterView for the header.

How to avoid Reusable Cell's copy behavior

Here is the problem, when I use a TableViewController and add a behavior on cell been selected. The behavior showed twice
How can I avoid this?
// MARK: - Table Deleget
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
UIView.animate(withDuration: 0.2, animations: {
cell?.viewWithTag(100)?.isHidden = true
(cell?.viewWithTag(200) as! UILabel).textColor = UIColor.red
})
}
override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
UIView.animate( withDuration: 0.3, animations: {
cell?.viewWithTag(100)?.isHidden = false
(cell?.viewWithTag(200) as! UILabel).textColor = UIColor(red: 0, green: 128/255, blue: 128/255, alpha: 1)
})
}
Move the animation from the 'cellForRow' method to 'willDisplayCell' method. I think it can help to you avoid the twice animation.
I have fixed it, add a var to remember the cell which has been taped, and use cellWillDisplay to refresh every cell will displayed, check each cell if it has been selected, if has, show it the selected way.
// MARK: - Table Deleget
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
index = indexPath.row
if let cell = tableView.cellForRow(at: indexPath){
UIView.animate(withDuration: 0.2, animations: {
cell.viewWithTag(100)?.isHidden = true
(cell.viewWithTag(200) as! UILabel).textColor = UIColor.red
})
}
}
override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) {
UIView.animate( withDuration: 0.3, animations: {
cell.viewWithTag(100)?.isHidden = false
(cell.viewWithTag(200) as! UILabel).textColor = UIColor(red: 0, green: 128/255, blue: 128/255, alpha: 1)
})
}
}
// Added this
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if let index = index, index == indexPath.row {
cell.viewWithTag(100)?.isHidden = true
(cell.viewWithTag(200) as! UILabel).textColor = UIColor.red
} else {
cell.viewWithTag(100)?.isHidden = false
(cell.viewWithTag(200) as! UILabel).textColor = UIColor(red: 0, green: 128/255, blue: 128/255, alpha: 1)
}
}

Target a cell - Strikethrough to Mark Task Item as Complete - Swift 3

I am a total newbie to Swift and XCode. I am in the process of creating a to do list application. I am attempting to mark a task as complete when a user swipes the cell in the table and clicks a "complete" button, but I am having difficulty targeting a specific table cell in order to do this. I found this example: UILabel with text struck through (see comment by MicroR), which helped me actually figure out how to do a strikethrough on a given piece of text, and I have this basic idea of strikethrough working if I just add it in when I create my table cells in my cellForRowAtIndexPath function. The issue is that I would like to only mark a task as complete when a user clicks the complete button after swiping on that specific cell in the table. I need some help with the code for targeting a cell after the user pushes the "complete" button (this function is the last one in the code below, but I included the rest of the code in this function for clarity). Thank you!
Here is the code that allows a strikethrough to happen when I put it in my cellForRowatIndexPath function
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: message)
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))
Here is my code:
// I pull in fake data right now from another function - sentData1 contains my task item.
var sentData1:String!
var sentData2:String!
var sentData3:String!
var sentData4:String!
var sentData5:String!
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
self.navigationItem.title = sentData5
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.section [section]
}
override func numberOfSections(in tableView: UITableView) -> Int {
return self.section.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Task", for: indexPath)
cell.selectionStyle = .none
switch (indexPath.section)
{
case 0:
cell.textLabel?.text = sentData1
case 1:
cell.textLabel?.text = sentData2
case 2:
cell.textLabel?.text = sentData3
case 3:
cell.textLabel?.text = sentData4
default:
cell.textLabel?.text = "Other"
}
return cell
}
override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let delete = UITableViewRowAction(style: .normal, title: "Delete") { action, index in
print("delete button tapped")
}
delete.backgroundColor = UIColor(red: 27/255, green: 124/255, blue: 150/255, alpha: 1.0)
let edit = UITableViewRowAction(style: .normal, title: "Edit") { action, index in
print("edit button tapped")
}
edit.backgroundColor = UIColor(red: 130/255, green: 208/255, blue: 216/255, alpha: 1.0)
let markComplete = UITableViewRowAction(style: .normal, title: "Complete") { action, index in
// this is where I want to target the cell!
print("complete button tapped")
}
markComplete.backgroundColor = UIColor(red: 0/255, green: 66/255, blue: 89/255, alpha: 1.0)
return [edit, delete, markComplete]
}
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool
{
return true
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
}
:)
I am a total newbie to Swift and XCode too..
My english is no good too :)
but if i understood you.
I think that the answer to your question is..:
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]?
{
let delete = UITableViewRowAction(style: .normal, title: "Delete") { action, index in
self.Apertou_Botao(pIndice: indexPath.row, pTexto: "Apertou Deletar")
}
delete.backgroundColor = UIColor(red: 27/255, green: 124/255, blue: 150/255, alpha: 1.0)
return [delete]
}
func Apertou_Botao(pIndice : Int , pTexto : String)
{
print("\(pTexto) indice Clicado..: \(pIndice)");
}
}

Swift UITableView visually deselects cells on scroll

I made an app that lists items in a UITableView. When I select items and scroll down till they go off screen, they will become visually deselected, meaning:
The checkbox image we set and the backgroundcolor are reset to original state.
The system itself however, does actually know what was selected and what wasnt.
Code:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:TblCell! = self.tableView.dequeueReusableCellWithIdentifier("cell") as TblCell!
cell.lblCarName.text = tableData[indexPath.row]
cell.lblPrice.text = tablePrice[indexPath.row]
if (tableAvailability[indexPath.row] == "NO") {
cell.imgCarName.image = UIImage(named: "nonselectable")
cell.lblPrice.textColor = UIColor(red: 172/255, green: 76/255, blue: 67/255, alpha: 1);
} else {
cell.imgCarName.image = UIImage(named: "deselected")
}
cell.selectionStyle = UITableViewCellSelectionStyle.None;
return cell
}
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
let cell:TblCell = tableView.cellForRowAtIndexPath(indexPath) as TblCell
if (tableAvailability[indexPath.row] == "YES") {
println("Row \(indexPath.row) selected")
//var myBackView = UIView(frame: cell.frame)
cell.backgroundColor = UIColor(red: 190/255, green: 225/255, blue: 255/255, alpha: 1);
//cell.selectedBackgroundView = myBackView
cell.imgCarName.image = UIImage(named: "selected")
}
}
func tableView(tableView: UITableView!, didDeselectRowAtIndexPath indexPath: NSIndexPath!) {
let cell:TblCell = tableView.cellForRowAtIndexPath(indexPath) as TblCell
if (tableAvailability[indexPath.row] == "YES") {
println("Row \(indexPath.row) deselected")
//var myBackView = UIView(frame: cell.frame)
cell.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1);
//cell.selectedBackgroundView = myBackView
cell.imgCarName.image = UIImage(named: "deselected")
}
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 70
}
Any idea on how to fix this?
thanks in advance.
On 'didSelectRowAtIndexPath', you change the backgroundColor and imgCarName straight on the cell.
When you scroll, your cell gets reused! Meaning that the same cell is destroyed and used to present new content.
To keep track of what is selected, you need to save that state somewhere else than against the cell, maybe in your tableAvailability object, or any other object that handles the content of your cells.

Resources