My data changes in UITableViewCell when I scroll down and get back
it is because your cells are being reused :
// this line initialize a cell of type shopsTableViewCell
//if no cell can be reused, get an already used cell else
let cell : shopsTableViewCell
= tableView.dequeueReusableCellWithIdentifier("shopCell",
forIndexPath: indexPath) as! shopsTableViewCell
so image and other data should be the last image and data of the dequeued cell.
To avoid this, you shall implement prepareForReuse
in the cell class file to reset data and image. docs of apple about prepareForReuse func
From Apple docs :
If a UITableViewCell object is reusable—that is, it has a reuse identifier—this method is invoked just before the object is returned from the UITableView method dequeueReusableCellWithIdentifier:. For performance reasons, you should only reset attributes of the cell that are not related to content, for example, alpha, editing, and selection state. The table view's delegate in tableView:cellForRowAtIndexPath: should always reset all content when reusing a cell. If the cell object does not have an associated reuse identifier, this method is not called. If you override this method, you must be sure to invoke the superclass implementation.
//Example :
override func prepareForReuse() {
super.prepareForReuse()
//Do reset here
this.shopImg?.image = nil
this.shopName?.text = ""
this.shopDescription?.text = ""
}
UITableViewCell Data changes after scrolling
Create UITableViewCell
subclass and override prepeareForReuse
function - to turn cell to default mode.
Swift:
override func prepareForReuse() {
super.prepareForReuse()
//set cell to initial state here, reset or set values, etc.
}
As per your comment - of how to subclass UITableViewCell
:
import UIKit
class MyCustomCell: UITableViewCell {
// things that you can do here:
// initialise your properties here. (label, textfield. etc)
// layout subviews.
// override superclass APIs.
}
Selection of UITableViewCell Changes when Scroll down in Swift
cellForRowAt will be called every time that cell is displayed.
you need selected list to save selected index.
var listSelected: [Int] = []
and
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TasksTableViewCell") as! TasksTableViewCell
cell.backView.backgroundColor = listSelected.contains(indexPath.row) ? UIColor(named: "primaryViewColor") : .white
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if listSelected.contains(indexPath.row) {
listSelected = listSelected.filter{$0 != indexPath.row}
} else {
listSelected.append(indexPath.row)
}
tableView.reloadRows(at: [indexPath], with: .automatic)
}
Swift UITableView Cell changes image when scrolled
You need to save the change of state in your data model somewhere, and then edit your data source methods (specifically cellForRowAtindexPath) to show the cell in its new selected state.
This is true of any change you make to a cell. If the cell scrolls off-screen, it gets recycled and any customizations you've made to it's views will not longer be associated with that indexPath any more. So when the user takes action on a cell, you always need to record the information in your data model.
Value changes on scroll UITableView
In your UITableViewCell
PostCellImage
subclass you should override prepeareForReuse
function - to turn cell to default mode.
Swift:
override func prepareForReuse() {
super.prepareForReuse()
//set cell to initial state here
//set like button to initial state - title, font, color, etc.
}
Tableview cell setting keep changed when I scroll down and get back swift
The reason for this is cell reuse.
It means cells that have already been styled in a certain way can be reused to display different data. You have to make sure that you setup every aspect of the styling each time a cell is reused to avoid cross over.
It might be easier to keep track of the styling if you make a configureCell
method to handle it, this way you can't forget to set one value and you could even set some default values.
func configureCell(cell:RosterCell,
textColour:UIColor,
seperatorColour:UIColor,
buttonEnabled:Bool,
buttonAlpha:CGFloat,
interactionEnabled:Bool = false) {
cell.separator.backgroundColor = seperatorColour
cell.jobDetail.textColor = textColour
cell.button.isEnabled = buttonEnabled
cell.button.alpha = buttonAlpha
cell.isUserInteractionEnabled = interactionEnabled
}
Then your cellForRowAt
method would look like this
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "rosterId", for: indexPath) as! RosterCell
...
if jobCancel[indexPath.section] == 0{
configureCell(cell: cell,
textColour: UIColor.gray,
seperatorColour: UIColor.darkGray,
buttonEnabled: false,
buttonAlpha: 0.3,
interactionEnabled: true)
}
if cell.jobDetail.text == "Cancelled Schedule"{
configureCell(cell: cell,
textColour: UIColor.gray,
seperatorColour: UIColor.red,
buttonEnabled: false,
buttonAlpha: 0.3)
}
if cell.jobDetail.text == "Off Day"{
configureCell(cell: cell,
textColour: UIColor.gray,
seperatorColour: UIColor.darkGray,
buttonEnabled: false,
buttonAlpha: 0.3)
}
}
As you can see I have set the interactionEnabled
parameter to default to false
so I did not need to set it for the last two if statements.
Images in UITableViewCell change after scrolling down and back up
In your table view cell PostTableViewCell you need to implement the method
override func prepareForReuse() {
super.prepareForReuse()
self.postImageView.image = nil
// Set cell to initial state here, reset or set values
}
The cells are holding on to their old content
Swift UITableViewCell - Cell Image changes if I scroll down too fast, but only on the first attempt
This occurs because of
1- cell dequeueing : cells are re-used inside the tableView
2- when you scroll before a request happens it may cause a new 1 with same url
Best option is using SDWebImage
When I scroll down in my TableView, the cell data changes - Swift 4
You update number
every time the table view asks for a cell. That has no direct relation to the row being displayed.
It's unclear why you even have the number
property.
If you just want to show the corresponding row number in each cell, get rid of the number
property and update:
cell.textLabel?.text = "\(number)"
with:
cell.textLabel?.text = "\(indexPath.row)"
Related Topics
Check for Value or Reference Type in Swift
Understanding Shorthand Closure Syntax for Map Function in Swift
Handle Swiftui and Corelocation with Mvvm-Pattern
Drawing a 3D Arc and Helix in Scenekit
Deploy App with Pre-Populated Core Data
Creating a Custom Scngeometry Polygon Plane with Scngeometryprimitivetype Polygon Crash/Error
Adding a Search Bar to Navigationview in Swiftui
Use Uipangesturerecognizer to Drag Uiview Inside Limited Area
Swift Didset Get Index of Array
Open File Dialog Crashes in Swift
Swiftui - Foreach Deletion Transition Always Applied to Last Item Only
"Cannot Inherit from Non-Open Class" Swift
How to Set an Attributed Title Color for State in Swift
Calling a Global Function Which Has the Same Name as a Member Function
How to Check If 'Any(Not Any)' Is Nil or Not in Swift
Convert Dictionary to Query String in Swift