danu
danu

Reputation: 1178

A sub view in UITableview gets disappear once the row is been clicked.

I'm working on a project in swift 3.0 where I have a UIView inside a UITableViewCell. For some reason once a row is been selected the view gets disappear, and once a a new row is been selected the view of the previous cell shows up and the view of the newly selected one gets disappear.Name of the view that gets disappear is redView(reference of the code bellow). My code and UITableView delegates as follow.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
     let cell =  tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    let backgroundView = UIView()
            backgroundView.backgroundColor = UIColor.clear
            cell.selectedBackgroundView = backgroundView

            let data = self.mediaList[indexPath.row] as! NSDictionary
    let redView = cell.viewWithTag(TABLE_CELL_TAGS.redView)!
            let reducedPriceLabel =  cell.viewWithTag(TABLE_CELL_TAGS.reducedpricelable) as! UILabel
             Utils.setTableCellLabelText(cell: cell, labelTag: TABLE_CELL_TAGS.title, text: data["title"] ?? "title...")

     if let reducedPrice = data["mediaValue"] as? Int{
                   reducedPriceText = "$\(String(reducedPrice))"
                    redView.isHidden = false
                    reducedPriceLabel.isHidden = false
                }else{
                redView.isHidden = true
                reducedPriceLabel.isHidden = true
                }
                Utils.setTableCellLabelText(cell: cell, labelTag: TABLE_CELL_TAGS.reducedpricelable, text: reducedPriceText)

            return cell;

}



func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

   let mP3ViewController = self.storyboard?.instantiateViewController(withIdentifier:"MP3ViewController") as! MP3ViewController
        mP3ViewController.mediaDetails = mediaList[indexPath.row] as? NSDictionary
        self.navigationController?.pushViewController(mP3ViewController, animated:true)
}

Upvotes: 0

Views: 814

Answers (2)

Rakesh Salian
Rakesh Salian

Reputation: 159

Go to the storyboard setting of tableViewcell set selection to None. then I think it will work for you.

enter image description here

Upvotes: 3

Satish Babariya
Satish Babariya

Reputation: 3096

Try this and let me know.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

 tableView.deselectRow(at: indexPath, animated: true)

   let mP3ViewController = self.storyboard?.instantiateViewController(withIdentifier:"MP3ViewController") as! MP3ViewController
        mP3ViewController.mediaDetails = mediaList[indexPath.row] as? NSDictionary
        self.navigationController?.pushViewController(mP3ViewController, animated:true)
}

Upvotes: 1

Related Questions