Henrique Dourado
Henrique Dourado

Reputation: 320

Why isn't the cell's selection being recognized? Swift

I need help with that. I'm trying to set a blurred view to appear when I click on a cell. I don't know if there is any function to detect if the cell is touched so I'm using the "selected" boolean value to check if it is selected/touched. After that, if there is an image in the image array I'd like to show it in the blurred view. A lot of apps to that, probably not like this but thats the only way I found. When I tested it, the selection is still appearing, but nothing is happening, like this code was never executed. If even the alert showed up I would know it is working, but it's not.

Observations: The cellImgView is a UIView I've added to put the image description, the image and a close button and the blur view mainly. All of those are set to hidden in the interface builder cause I just want to unhide them when a cell is touched.

THIS CODE IS INSIDE THE FUNCTION:

   tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

Here is the code

if cell.selected == true { //!
            if let dataArr = dft.valueForKey("images") as? [NSData] {


                let nowImgData = dataArr[indexPath.row]
                let nowImg = UIImage(data: nowImgData)

                let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark)
                let blurView = UIVisualEffectView(effect: blurEffect)
                theCellView.addSubview(blurView)
                blurView.frame = theCellView.bounds

                theCellView.hidden = false
                theCellImgViewCloseBtn.hidden = false
                theCellImageDesc.hidden = false
                theCellImage.hidden = false

                theCellImage.image = nowImg
                theCellImageDesc.text = descriptions[indexPath.row]
            } else {
                let alert = UIAlertController(title: "No image", message: "This cell has no image", preferredStyle: UIAlertControllerStyle.Alert)
                alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
                presentViewController(alert, animated: true, completion: nil)
            }
        }

Any help is appreciated!

Upvotes: 0

Views: 53

Answers (1)

Shen
Shen

Reputation: 644

Take a look at:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

}

Or even better, right click on your UITableViewDelegate inheritance and Jump To Definition to go through the available methods you have at your disposal. It's good to know what you have available to you.

Upvotes: 2

Related Questions