Image not changing on didSelectRowAt - ios

I've used a collection view and one table view in my view and on didSelect function image is not changing.
Below is the code :
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell:list2TableCell = list2Table.dequeueReusableCell(withIdentifier: "Cell") as! list2TableCell
let image = UIImage(named: "fill-square")
cell.tickImage.image = image
}

Try:
if let cell:list2TableCell = list2Table.cellForRow(at: indexPath) as? list2TableCell{
let image = UIImage(named: "fill-square")
cell.tickImage.image = image
}

First of all, all you're doing is making a new cell that lives nowhere and changing its image — and then throwing away the cell. That's pretty silly! What you want to do is change the image of the cell that lives at the indexPath you were given.
Second, even doing that would be wrong. What you must do is change the image in your data model and then tell your table view to reloadData, so that cellForRowAt will be called and it will set the image in the cell.

Related

Why are UIImage's partially loaded in the TableViewCells?

I have a table view. First, I load text data for all cells from database. Next, I load image for every cell from another database. But when I tried to set images, they are not displayed.
If I use
let cell = myTableView.dataSource?.tableView(myTableView,
cellForRowAt: indexPath) as? MyTableViewCell
then images loaded only in invisible cells. It means that those cells that I see the very first (before scrolling down) don't show their images. But all others do.
Another way, if I use
let cell = myTableView.cellForRow(at: indexPath) as? MyTableViewCell
then the opposite happens - only those cells that I see show their images, but all others don't.
After that I do
cell.setImage(image: image)
func setImage(image: UIImage) {
guard let myImageView = myImageView else { return }
myImageView.image = image
}
I set images not in func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {...} because as I said, images come after creating cells.
You need to refresh the Table View in the main thread after the load. You can do it with:
DispatchQueue.main.async {
self.yourTableView.reloadData()
}

Swift UITableView Cell changes image when scrolled

I have a image inside my tableview cells that turns black when the cell is selected as well as the default cell turning gray. Problem is when I scroll down the screen and scroll up again the images are turn back as though they are not selected being image "cellSelected". So, after scrolling selected cells should have the image "cellSelected" yet they have "cellNotSelected". How can I make sure my app remembers the selected cells images?
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! CustomerSelectsBusinessesCell
cell.selectedCell.image = UIImage(named: "cellSelected")
updateCount()
}
override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! CustomerSelectsBusinessesCell
cell.selectedCell.image = UIImage(named: "cellNotSelected")
updateCount()
}
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.

Can't make ImageView a circle in a Custom Table View Cell

I am trying to display a rounded image for every custom cell in a table view. I am already changing its cornerRadius, but for some reason the image still will appear fully - even when the ImageView's borders are already set round.
Here's a piece of the code:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> CustomTableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "celda", for: indexPath) as! CustomTableViewCell
cell.imgView.layer.borderWidth = 1
cell.imgView.layer.borderColor = UIColor.black.cgColor
cell.imgView.layer.cornerRadius = cell.imgView.frame.height/2
//...the rest of function
//image is added to imgView eventually
}
And here is the result in the sim
I tried putting the code in override func awakeFromNib in the cell file but I had the same result.
Any advice is welcome :)
Please try this code by following step:
step1:
Make the height and width same of image view. Add the 1:1 ratio in storyboard in image view for equal height and width.
step2:
cell.imgView.layer.cornerRadius = cell.imgView.frame.height/2
cell.imgView.clipsToBounds = true
It may helps you.Thank you.

Dynamic Table Image View in Swift

I have a very similar question as posted here (Dynamic UIImageView Size Within UITableView) where I'm trying to dynamically retrieve an image from Firebase and make the resulting tableview adjust to the height of the image given a fixed width across the screen based on the aspect ratio. All the articles I read says to make the cell calculation based on cellforRowAt, but my actual image is within the TableViewCell. Can someone please help?
Tableview controller:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "FeedTableViewCell", for: indexPath) as! FeedTableViewCell
cell.configureCell(post: postArray[indexPath.row])
return cell
}
TableViewCell:
func configureCell(post: Post){
self.postImage.sd_setImage(with: URL(string: post.postImageURL),
placeholderImage: UIImage(named: "default"))
}
you have cell.setNeedsLayout() inside the completion handler of setting image method, like this:
cell. postImage.kf.setImage(with: URL) { _, _, _, _ in
cell.layoutIfNeeded()
}
First of all, you need to use Autolayout to calculate the proper cell height by creating a proper set of constraints that would determine the cell height based on content. I am going to assume you did that.
Then you have to tell the tableView you want it to use Autolayout to calculate height:
// my best estimation of the height
tableView.estimatedRowHeight = 144
// but the real height is calculated by autolayout
tableView.rowHeight = UITableViewAutomaticDimension
Now this would work if the autolayout could calculate the height correctly in cellForRowAt. But since the image is downloaded asynchronously, the image is set later, when the cell may be already presented. This requires you to provide a way in which a cell can tell the tableView that it has downloaded its content and its layout needs to be recalculated. To do so, use this method in the viewController with the tableView:
func recalculateTableViewLayout() {
self.tableView.beginUpdates()
self.tableView.setNeedsLayout()
self.tableView.endUpdates()
}
You will need to pass the reference to the viewController with the tableView to each cell (I recommend to use delegate pattern for that, here for brevity I will simply sketch it using it directly):
class FeedTableViewCell: UITableViewCell {
weak var feedViewController: FeedViewController?
// etc.
And in the cellForRowAt:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "FeedTableViewCell", for: indexPath) as! FeedTableViewCell
cell.feedViewController = self
cell.configureCell(post: postArray[indexPath.row])
return cell
}
Then use completion handler of sd_setImage to tell the tableView to recalculate its layout when the image gets downloaded:
func configureCell(post: Post){
self.postImage.sd_setImage(with: URL(string: post.postImageURL), placeholderImage: UIImage(named: "default")) { (image, error, cache, url) in
self.feedViewController?.recalculateTableViewLayout()
}
}

Standard TableView Cell - label text overlaps with uiimage

This is happening but I don't know why. It is a standard tableviewcell with a label and an image on it
Here is the code:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = patientsTableView.dequeueReusableCell(withIdentifier: "patientTableViewCell", for: indexPath)
let text = "Bob Marley"
cell.textLabel?.text = text
return cell
}
When I do not put an image view in the object inspector but like below, it solves the problem. I am not sure why though
cell.imageView?.image = UIImage(named: "patients")

Resources