Reputation:
I have uitableview
with 10 images in rows in my Xcode swift project. I use this code to show 10 images in rows in my uitableview
:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: String(format: "Cell0", indexPath.row), for: indexPath)
var imageV = UIImageView()
imageV = cell.viewWithTag(5) as! UIImageView
imageV.image = UIImage(named:"image\(indexPath.row + 1).png")
But when I scroll my uitableview
freezing very very very hard. Why is this happening? And How to fix it?
Upvotes: 1
Views: 1062
Reputation: 3802
Create a custom cell
class MyCustomCell: UITableViewCell {
@IBOutlet weak var imgView: UIImageView!
}
In your storyboard, set the class of the cell to MyCustomCell
and give it an identifier say cell
.
Then, update your cellForRow
method as below
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyCustomCell
let imageName = "image\(indexPath.row + 1).png"
cell.imgView.image = UIImage(named: imageName)
return cell
}
UPDATE
MyCustomCell.swift
MyCustomCell
UIImageView
in your cell
@IBOutlet
cellForRow
method
Upvotes: 2
Reputation: 2650
let cell = tableView.dequeueReusableCell(withIdentifier: String(format: "Cell0", indexPath.row), for: indexPath)
to
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as CustomCell
cell.imageView.image = UIImage(named: "yourimagename")
Don't change your reusable identifier everytime. This is causing freezing in your case. Moreover create custom cell for adding UIImage.
Upvotes: 0
Reputation: 1142
The tableview is lagging and freezing on the scrolling because of how CellForRow works. Essentially, the OS does not save cells that are displayed. For example, rows 1-2-3 are displayed on the screen and have gone through cellForRow. Rows 4 is about to be displayed and row 1 will be removed off the screen. Row 4 will go through cellForRow for it's information to be displayed, and once row 1 is off the screen it will be removed from memory. So if you then scroll back to row 1, it will go through cellForRow and reload the image. So in this case your tableview is constantly loading images from the bundle as you scroll up and down.
Your solution might be to preload all your images and have them in an array so that the OS doesn't have to spend time loading the images repeatedly. Another way to go about might be to create a custom cell and simply have the cell handle loading it's own image by providing it the image name, rather than have the tableview manage the loading of every single's cell's image repeatedly. This would also clean up your cellForRow.
For example: make a new tableViewCell called imageCell which has an imageName property that could be an String. Once the cell is made, you simply pass it the string of it's image in cellForRow, and when the imageCell is created, have it load it's own image through this string in it's awakeFromNib.
Upvotes: 0