Swift TableViewCell xib has no limits

I have two TableViews, when I developed the original, which I developed using the prototype cell in the storyboard to make it reusable, I tried to pull it in .xiband load it instead. When it loads from cellID.xib, however, it loses all restrictions at runtime, and everything overlaps.

Tableviewcontroller

let cellIdentifier = "cellID"
override func viewDidLoad() {
    super.viewDidLoad()

    tableView.register(UINib(nibName: cellIdentifier, bundle: nil), forCellReuseIdentifier: cellIdentifier)
    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 300
}

Storyboard cell in storyboard (used for work)

prototype cell

Cell when copying to XIB

xib cell

Limitations

contraints on cell

XIB View Hierarchy

xib view hierarchy

+2
source share
4 answers

Problem

, , "Swift TableViewCell xib ". , , :

, , xib, xib, .

, :

  • , TableViewCell
  • / Storyboard xib

№1 , xib, . .

№2 , . , , , . / Storyboard xib, . , / .

-

, . . . View . 1- 2- MyCell. 3- 4- MyCopyPasteCell. 5- 6- MyPrototypeCell.

MyCell xib , . , . MyCopyPasteCell / Storyboard xib. MyPrototypeCell - , . , xib. xib .

enter image description here

, , ContentView. ( , height=20, aspect=1:1 height=75, .) , . .

// bottomMargin = Profile Image View.bottom + 188.5
bottomMargin = Profile Image.bottom + 17
Profile Image View.top = Username Label.top
Profile Image View.leading = leadingMargin + 2
Profile Image View.top = topMargin + 20
Username Label.leading = Content Text View.leading    
// Username Label.top = topMargin + 20        
Username Label.trailing = Content Text View.trailing
Username Label.leading = Profile Image View.trailing + 15
trailingMargin = Username Label.trailing + 10
Content Text View.top = Username Label.bottom + 5
Content Text View.leading = leadingMargin + 92
bottomMargin = Content Text View.bottom + 20

// bottomMargin = Profile Image View.bottom + 188.5 , 188.5. Prototype ( ). bottomMargin = Profile Image.bottom + 17, 188.5 17.

// Username Label.top = topMargin + 20 ( topMargin 20) . Profile Image.top = topMargin + 20 ( topMargin 20) Profile Image View.top = Username Label.top ( , .. 20).

MyTableViewController

. / . MyCell MyCopyPasteTableViewCell xib viewDidLoad(). MyPrototypeCell viewDidLoad().

//  MyTableViewController.swift
import UIKit    
class MyTableViewController: UITableViewController {

    let myCell = "MyCell"
    let myCopyPasteCell = "MyCopyPasteTableViewCell"
    let myPrototypeCell = "MyPrototypeCell"

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(UINib(nibName: myCell, bundle: nil), forCellReuseIdentifier: myCell)
        tableView.register(UINib(nibName: myCopyPasteCell, bundle: nil), forCellReuseIdentifier: myCopyPasteCell)
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 300
    }

    // MARK: - Table view data source
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 3
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: myCell, for: indexPath)
            return cell
        } else if indexPath.section == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: myCopyPasteCell, for: indexPath)
            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: myPrototypeCell, for: indexPath)
            return cell
        }
    }
}

Github

https://github.com/starkindustries/TableViewCellConstraintsTest

+4

, Autolayout XIB, ON File Inspector

enter image description here

Xib 😊

1. UITableCell .xib (✅ Xib) enter image description here

2.

enter image description here

3.

🔸 ImageView contarints Top, Left Bottom      [ Border ]

🔸 UIlable title lable       Top, Left Right NumberOflines=0 (🚫 - ⚠️ , )

🔸 UIlable Descriptions lable NumberOflines=0      (⚠️ UIlable )

. enter image description here

enter image description here  

4. UIViewController

 @IBOutlet var tableView: UITableView!

    var cellIdentifier="cell"
    var cellXibName = "MyTableCell"


    override func viewDidLoad() {
        super.viewDidLoad()


        tableView.register(UINib(nibName: cellXibName, bundle: nil), forCellReuseIdentifier: cellIdentifier)
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 320


    }



    func numberOfSections(in tableView1: UITableView) -> Int {
        return 3
    }

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

            let cell :MyTableCell = tableView1.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! MyTableCell


            return cell
    }

, , ,

enter image description here

, ... , , .

+2

:

. , .

uiview . enter image description here

enter image description here

:

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
    }

}

: enter image description here

0

, - , ? - .

XIB files:

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            return 300
}

, , . , , 100% ? UIImageView height width. . ( width Aspect Ratio)

-1

Source: https://habr.com/ru/post/1679858/


All Articles