【问题标题】:A sub view in UITableview gets disappear once the row is been clicked.一旦单击该行,UITableview 中的子视图就会消失。
【发布时间】:2018-02-02 00:20:54
【问题描述】:

我正在 swift 3.0 中开发一个项目,其中我在 UITableViewCell 中有一个 UIView。由于某种原因,一旦选择了一行,视图就会消失,一旦选择了新行,就会显示前一个单元格的视图,而新选择的单元格的视图就会消失。消失的视图的名称是 redView (参考下面的代码)。我的代码和 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)
}

【问题讨论】:

标签: swift uitableview swift3


【解决方案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)
}

【讨论】:

  • 仍然在单击该行时消失
  • 尝试重装tableview in viewwillappear
【解决方案2】:

转到 tableViewcell 的情节提要设置,将选择设置为 None。那么我认为它对你有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多