使用自定義尺寸初始化時布局單元子視圖 - Layout cell subviews at initialization using custom dimensions -开发者知识库

使用自定義尺寸初始化時布局單元子視圖 - Layout cell subviews at initialization using custom dimensions -开发者知识库,第1张

Question - short version

問題 - 簡短版本

Despite configuring my cell height to be 112, my cells get instantiated at the standard height of 44. This messes up my subviews unless I use extra resources and call cell.layoutSubviews

盡管將我的單元格高度配置為112,但我的單元格在標准高度44處被實例化。這會弄亂我的子視圖,除非我使用額外的資源並調用cell.layoutSubviews

Context:

語境:

I made a subclass of UITableViewCell called ResearchCell, where in the init, I do the following:

我創建了一個名為ResearchCell的UITableViewCell的子類,在init中,我執行以下操作:

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    print(NSStringFromCGRect(self.frame)) // prints out the standard 320*44 cell
    manageViews()
}

I am using a xib for my cell:

我正在為我的手機使用xib:

使用自定義尺寸初始化時布局單元子視圖 - Layout cell subviews at initialization using custom dimensions -开发者知识库,enter image description here,第2张

My Setup so far

我的設置到目前為止

In the ViewController, I call the following in viewDidLoad:

在ViewController中,我在viewDidLoad中調用以下內容:

researchTableView.dataSource = researchDataSource
researchTableView.delegate = self
researchTableView.register(ResearchCell.self, forCellReuseIdentifier: "ResearchCell")

I also call the following:

我也稱之為:

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

In researchDataSource, I do the following:

在researchDataSource中,我執行以下操作:

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

    let cell = tableView.dequeueReusableCell(withIdentifier: "ResearchCell", for: indexPath) as! ResearchCell
    cell.topLineLabel?.text = "Hola"

    cell.layoutSubviews() //At the moment I have to call this to re-layout the views
    return cell
}

Problem

問題

Unless I call cell.layoutSubviews() (see comment above), the cell automatically assumes the standard size of 320*44, and all the views I have programmatically laid out in the cell are all jumbled up. The cell should be a height of 112, which I have specified in both my xib and the tableView.delegate. However, why is it not carried forth to cell creation?

除非我調用cell.layoutSubviews()(參見上面的注釋),否則單元格會自動采用320 * 44的標准大小,並且我在程序中以單元格布局的所有視圖都是混亂的。單元格應該是112的高度,我已經在我的xib和tableView.delegate中指定了它。但是,為什么不進行細胞創造?

Any pointer is appreciated - thanks!

任何指針都很感激 - 謝謝!

1 个解决方案

#1


2  

You are registering the class and not your custom nib. Change the following:

您正在注冊課程,而不是您的自定義筆尖。更改以下內容:

researchTableView.register(ResearchCell.self, forCellReuseIdentifier: "ResearchCell")

To:

至:

researchTableView.register(UINib(nibName: "ResearchCell", bundle: nil), forCellReuseIdentifier: "ResearchCell")

This will then load the nib from your bundle instead of instantiating the class.

然后,這將從您的包中加載nib,而不是實例化該類。

最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复