gpt4 book ai didi

ios - 自定义 UITableViewCell 只显示一个 UILabel,即使 Storyboard上有两个

转载 作者:行者123 更新时间:2023-11-28 10:51:43 28 4
gpt4 key购买 nike

我正在使用 Controller 制作一个简单的联系人应用程序:ContactTableViewController 和客户单元格:ContactTableViewCell。我创建了一个自定义表格 View 单元格,其中样式是自定义的,标识符ContactTableViewCell,类也是ContactTableViewCell。该单元格有两个 UILabel 字段,它们暴露给 ContactTableViewCell.swift 类,如下所示:

import UIKit

class ContactTableViewCell: UITableViewCell {

@IBOutlet weak var name: UILabel!
@IBOutlet weak var info: UILabel!

override func awakeFromNib() {
super.awakeFromNib()
print("cell loaded with name: ", name)
// Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)

// Configure the view for the selected state
}

}

现在我想在我的 ContactViewController 中显示它们,我的 Controller 的相关部分如下所示:

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

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


// here we communicate with parts of the app that owns the data
override func tableView(_ tableView : UITableView
, cellForRowAt indexPath: IndexPath
) -> UITableViewCell {

let id = "ContactTableViewCell"

// deque a cell as an instance of ContactTableViewCell
guard let cell = tableView.dequeueReusableCell(withIdentifier: id, for : indexPath)
as? ContactTableViewCell else {

fatalError("dequed cell is not instance of ContactTableViewCell")
}

cell.name.text = "hello"
cell.info.text = "hello information"

但是,当我运行模拟时,我只看到两行“hello”,尽管它应该是这样的:

hello
hello information

重复了两次。这里有什么问题?

最佳答案

很可能您没有足够的空间来放置这两个标签。您可以通过以下方式在 Storyboard 中设置行高:

  1. 选择您的表格 View
  2. 去找尺寸检验员
  3. 更改行高

Change row height

您可以在tableView 的委托(delegate)中实现tableView(_:heightForRowAt:) 方法。即

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
// return desired height for your cell
return 90.0
}

关于ios - 自定义 UITableViewCell 只显示一个 UILabel,即使 Storyboard上有两个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45908706/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com