Axel Cardinaels
Axel Cardinaels

Reputation: 168

Hide custom ViewTableCell Button for a specific cell

I'm facing a problem with a UITableViewCell.

When I generate my tableCells, I check if a value is equal to something and if so, I hide a custom button I made in my prototype cell. The problem is, the button with the identifier I hide is hidden in every Cell and not the current one only...

I looked around but can't find something to do... Could you help ? Here's my code, look at the line if currentPost.valueForKey... == "noLink". Thanks !

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        var cell = tableView.dequeueReusableCellWithIdentifier("postCell", forIndexPath: indexPath) as! PostTableViewCell
        var currentCell = indexPath
        var currentPost = post[indexPath.row]
        var currentUser = followedInfo[currentPost.valueForKey("userID") as! String]

        cell.username.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left


        cell.userProfil.layer.cornerRadius = 0.5 * cell.userProfil.bounds.size.width
        cell.username.setTitle(currentUser?.valueForKey("username") as? String, forState: UIControlState.Normal)
        cell.postArtist.text = currentPost.valueForKey("artistName") as? String
        cell.postDescription.text = currentPost.valueForKey("postDescription") as? String
        cell.postTitle.text = currentPost.valueForKey("songName") as? String
        cell.postTime.text = "7J"
        cell.postDescription.sizeToFit()

        if currentPost.valueForKey("itunesLink") as? String == "noLink" {

            cell.postItunes.hidden = true;

        }else{
            cell.postItunes.addTarget(self, action: "getToStore:", forControlEvents: .TouchUpInside)
        }

        if currentPost.valueForKey("previewLink") as? String == "noPreview" {
            cell.postPlay.alpha = 0;
        }else{
            cell.postPlay.addTarget(self, action: "getPreview:", forControlEvents: .TouchUpInside)
        }

        if currentPost.valueForKey("location") as? String == "noLocalisation" {
            cell.postLocation.text = "Inconnu"
        }else{
            cell.postLocation.text = currentPost.valueForKey("location") as? String
        }


        if currentUser?.valueForKey("profilePicture")! != nil{
            var pictureFile: AnyObject? = currentUser?.valueForKey("profilePicture")!
            pictureFile!.getDataInBackgroundWithBlock({ (imageData, error) -> Void in
                var theImage = UIImage(data: imageData!)
                cell.userProfil.image = theImage
            })
        }

        if currentPost.valueForKey("coverLink") as? String == "customImage" {
            var profilPictureFile: AnyObject? = currentPost.valueForKey("postImage")

            profilPictureFile!.getDataInBackgroundWithBlock { (imageData , imageError ) -> Void in

                if imageError == nil{
                    let image = UIImage(data: imageData!)
                    cell.postPicture.image = image
                }
            }
        }else{
            if currentPost.valueForKey("coverLink") as? String == "noCover"{

                var cover = UIImage(named: "noCover")
                cell.postPicture.image = cover

            }else{
                var finalURL = NSURL(string: currentPost.valueForKey("coverLink") as! String)
                let request: NSURLRequest = NSURLRequest(URL: finalURL!)
                NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in
                    if error == nil {
                        var image = UIImage(data: data)


                        cell.postPicture.image = image

                    }
                    else {
                        println("Error: \(error.localizedDescription)")
                    }
                })
            }
        }









        return cell;
    }

Upvotes: 0

Views: 875

Answers (1)

Icaro
Icaro

Reputation: 14845

You are not setting the bottom back to visible if you want it to show, because you are reusing the cell the last setup is part of the cell, so as soon as you set it to hidden to true it will still hidden until you set hidden back to false, the best way to use reusable cell is to always set all the options to what you want to be for that cell to avoid the setting to be as the old cell.

    if currentPost.valueForKey("itunesLink") as? String == "noLink" {
        cell.postItunes.hidden = true;
    }else{
        cell.postItunes.hidden = false;
        cell.postItunes.addTarget(self, action: "getToStore:", forControlEvents: .TouchUpInside)
    }

Upvotes: 3

Related Questions