Swift在单元格中隐藏/显示UIView(来自数据库的数据)

时间:2018-05-30 13:29:05

标签: ios swift uitableview firebase firebase-realtime-database

我的UITableViewCell包含UITextView和UIView

我试图隐藏/显示UIView

enter image description here

约束 - TextView:

  

追踪到Superview Equals 2

     

导致Superview Equals 2

     

超级视图等于2

     

底部到超级视图等于40

的UIView:

  

追踪到超级视图

     

导致Superview

     

底部到超级视图

     

身高35

在Cell类中,我连接TextView BottomConstraint

UITableViewCell类:

 @IBOutlet weak var myTextView: UITextView!
 @IBOutlet weak var detailView: UIView!
 @IBOutlet weak var detailLabel: UILabel!
 @IBOutlet weak var TextViewConstraintToBottom: NSLayoutConstraint!

override func awakeFromNib() {
        super.awakeFromNib()

        detailView.isHidden = true
        if detailView.isHidden == true {
            TextViewConstraintToBottom.constant = 0
        } else {
            TextViewConstraintToBottom.constant = 40

        }
}

在带有UITableView的ViewController中:

    var handle: DatabaseHandle?
    var ref: DatabaseReference?

    var quoteList: [String] = []
    var songArray: [String] = []

    override func viewWillAppear(_ animated: Bool) {
        myTableView.estimatedRowHeight = 100
        myTableView.rowHeight = UITableViewAutomaticDimension
    }

override func viewDidLoad() {
        super.viewDidLoad()

        myTableView.dataSource = self
        myTableView.delegate = self

        dataCatch()
}


func dataCatch() {

 ref = Database.database().reference()

        handle = ref?.child("Цитаты").child("\(cat)").child("\(quoteNAme)").observe(.childAdded, with: { (snapshot) in

        let username = snapshot.value as? NSDictionary

        if let us = username?["name"] {
                 self.quoteList.append(us as! String)
                 self.quoteList = self.quoteList.filter(){$0 != "1"}
                 self.myTableView.reloadData()
                 }

        if let us = username?["song"].unsafelyUnwrapped  {
                 self.celT?.detailView.isHidden = false
                 self.songArray.append(us as! String)
                 self.myTableView.reloadData()
                 }
                 })
}

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

let indexP = indexPath.row
cell.myTextView.text = quoteList[indexP]
cell.detailLabel.text = songArray[indexP]

return cell
}

在数据库中我有结构 enter image description here

我决定

在ViewController中:

func dataCatch() {

 ref = Database.database().reference()

        handle = ref?.child("Цитаты").child("\(cat)").child("\(quoteNAme)").observe(.childAdded, with: { (snapshot) in

        let username = snapshot.value as? NSDictionary

        if let us = username?["name"] {
                 self.quoteList.append(us as! String)
                 self.quoteList = self.quoteList.filter(){$0 != "1"}
                 self.myTableView.reloadData()
                 }

            if snapshot.hasChild("song") {
                let us = username?["song"]
                self.songArray.append(us as! String)

            } else {
                let tet = "1"
//                self.celT?.detailView.isHidden = true

                self.songArray.append(tet as! String)
            }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

cell.detailLabel.text = songArray[indexP]
        if cell.detailLabel.text != "1" {
            cell.detailView.isHidden = false
            cell.butConstraint.constant = 40
        } else {
            cell.detailView.isHidden = true
            cell.butConstraint.constant = 0
        }

1 个答案:

答案 0 :(得分:1)

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UI TableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! QuoteTableVC // hide = check your model whether it has song or not if hide { cell.TextViewConstraintToBottom.constant = 0 } else { cell.TextViewConstraintToBottom.constant = 40 } }

{{1}}