remove the subviews from the contentView of UITableViewCell (reset the UITableView)

后端 未结 9 1006
遥遥无期
遥遥无期 2020-12-23 15:29

Any idea on how to reset a UITableView?

I want to display a new set of data at the press of a button and also remove all the subviews from the cell\'s

9条回答
  •  南笙
    南笙 (楼主)
    2020-12-23 15:52

    you try with replace the content but not the UI, for example to a label replace the text:

    class CustomCell : UITableViewCell     {
    
        var firstColumnLabel = UILabel()
    
        override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
            super.init(style: style, reuseIdentifier: reuseIdentifier)
            firstColumnLabel = //create label
    
            contentView.addSubview(firstColumnLabel)
        }
    
        func setData(cad: String) {
            firstColumnLabel.text = cad
        }
    }
    

    and in your tableSource

     public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            var cell : UITableViewCell?
            cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
                (cell as! CustomCell).setData(cad: "new value")
            return  cell!
        }
    

提交回复
热议问题