0

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)
}
danu
  • 1,079
  • 5
  • 16
  • 48
  • Possible duplicate of [UITableViewCell subview disappears when cell is selected](https://stackoverflow.com/questions/6745919/uitableviewcell-subview-disappears-when-cell-is-selected) – Swifty Aug 24 '17 at 08:25
  • if thats the case how can I omit the complication – danu Aug 24 '17 at 08:39

2 Answers2

3

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

enter image description here

Rakesh Salian
  • 159
  • 10
1

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)
}
Satish Babariya
  • 3,062
  • 1
  • 15
  • 29