Rome Torres
Rome Torres

Reputation: 1081

IOS Swift how can I highlight a cell on tap with TableView recycling

I have a TableView and a label inside of it called Post. I have a Gesture Tap function and when a user taps that label then the TableView label that was tapped changes color. The issue is with the tableView recycling if I go down the tableView then other cells are also highlighted that were not clicked . How can I fix it so that only the clicked cell is highlighted ? This is my code

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "UserProfileTVC", for: indexPath) as! UserProfileTVC

        cell.post.text = Posts[indexPath.row]
        let post_tap = UITapGestureRecognizer(target: self, action: #selector(UserProfileC.post_tapped(sender:)))
        cell.post.addGestureRecognizer(post_tap)
        return cell
    }


    func post_tapped(sender:UITapGestureRecognizer) {

        let point = CGPoint(x: 0, y: 0)

        let position: CGPoint = sender.view!.convert(point, to: self.TableSource)
        if self.TableSource.indexPathForRow(at: position) != nil {
            sender.view?.backgroundColor = UIColor.blue

        }
    }

Again the code works and it highlights the correct TableCell label the issue is when scrolling down the tableView other tableCells label also get highlighted without being clicked .

I have updated the code with the sample given below but it is still giving the same results

if let existingRecognizerView = cell.viewWithTag(101) as UIView?  {

        existingRecognizerView.backgroundColor = UIColor.white


    } else {

let post_tap = UITapGestureRecognizer(target: self, action: #selector(UserProfileC.post_tapped(sender:)))
        post_tap.view?.tag = 101

    cell.post.addGestureRecognizer(post_tap)

    }

Upvotes: 2

Views: 1567

Answers (1)

toddg
toddg

Reputation: 2906

In your cellForRowAtIndexPath function you are dequeuing a cell that already has a UITapGestureRecognizer on it with a blue background. You will need to add a tag to its view so that you can get access to it when dequeuing and remove the background color.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "UserProfileTVC", for: indexPath) as! UserProfileTVC

    cell.post.text = Posts[indexPath.row]

    // Check if we already have a GestureRecognizer view
    if let existingRecognizerView = cell.post.viewWithTag(101) {
        existingRecognizerView.backgroundColor = UIColor.white
    } else {
        let post_tap = UITapGestureRecognizer(target: self, action: #selector(UserProfileC.post_tapped(sender:)))
        cell.post.addGestureRecognizer(post_tap)
        post_tap.view.tag = 101
    }

    return cell
}


func post_tapped(sender:UITapGestureRecognizer) {

    let point = CGPoint(x: 0, y: 0)

    let position: CGPoint = sender.view!.convert(point, to: self.TableSource)
    if self.TableSource.indexPathForRow(at: position) != nil {
        sender.view?.backgroundColor = UIColor.blue

    }
}

*Note: coded from a mobile device... not tested for syntactical or functional errors.

Upvotes: 1

Related Questions