2017-08-24 43 views
0

我正在使用swift 3.0在UITableViewCell中创建一个UIView。出于某种原因,一旦某行被选中,视图就会消失,一旦选择了一个新行,前一个单元的视图就会出现,新选择的视图将消失。获取消失的视图的名称是redView (参考下面的代码)。我的代码和UITableView代表如下。一旦单击该行,UITableview中的子视图就会消失。

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) 
} 
+0

的[被选择的小区时的UITableViewCell子视图消失]可能的复制(https://stackoverflow.com/questions/6745919/uitableviewcell-subview-disappears-when-cell-is-selected) –

+0

如果那该怎么才能省略并发症 – danu

回答

3

转到tableViewcell组选择的故事板设置。那么我认为它会为你工作。

enter image description here

1

试试这个,让我知道。

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) 
} 
+0

仍然点击行时它会消失 – danu

+0

尝试在viewwillappear中重新编码tableview –

+0

nope did not work – danu

相关问题