Swift TableViewCell xib doesn't have constraints

前端 未结 4 2152
眼角桃花
眼角桃花 2020-12-19 19:42

I have two TableViews, when I designed the original I designed it using the prototype cell in the storyboard, to make it reusable I tried to pull it out into a

4条回答
  •  悲哀的现实
    2020-12-19 20:32

    Solution :

    I was achive solution to your problem. hope it might be help to you

    I use uiview to clips all cell elements.

    And Cell Design

    Code :

    class ViewController: UIViewController {

    @IBOutlet var tableView: UITableView!
    
    
    
     override func viewDidLoad() {
            super.viewDidLoad()
    
            self.tableView.estimatedRowHeight = 100
            self.tableView.rowHeight = UITableViewAutomaticDimension
            self.tableView.register(UINib(nibName: "TestCell", bundle: Bundle.main), forCellReuseIdentifier: "TestCell")
    
            // Do any additional setup after loading the view, typically from a nib.
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
    
    }
    
    extension ViewController : UITableViewDataSource, UITableViewDelegate {
        func numberOfSections(in tableView: UITableView) -> Int {
            return 1;
        }
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 10
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "TestCell", for: indexPath) as! TestCell
    
            cell.selectionStyle = .none
    
            return cell
        }
    
    }
    

    Result :

提交回复
热议问题