gpt4 book ai didi

ios - UITableView 中的右侧细节不可见

转载 作者:行者123 更新时间:2023-11-30 13:50:22 26 4
gpt4 key购买 nike

我有一个 UIViewController,其中嵌入了 UITableView。因为我不想让 UIViewController 变得太重,所以我将 UITableViewDataSourceUITableViewDelegate

分开了
class ViewController: UIViewController {

@IBOutlet var tableView: UITableView!

var dataSource : UITableViewDataSource!
var tableDelegate: UITableViewDelegate!

override func viewDidLoad() {
super.viewDidLoad()

dataSource = TableViewDataSource()
tableDelegate = TableViewDelegate()

tableView.dataSource = dataSource
tableView.delegate = tableDelegate


// Do any additional setup after loading the view, typically from a nib.
}

}

class TableViewDataSource : NSObject, UITableViewDataSource {

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cellIdentifier = "MyCellIdentifier"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)

cell.textLabel?.text = "hello"
cell.detailTextLabel?.text = "world"

return cell
}
}
class TableViewDelegate : NSObject, UITableViewDelegate {
//custom code here if required


}

在我的 Storyboard 中,我在 UITableView 中创建了一个原型(prototype)单元,其标识符为

MyCellIdentifier

我使用此标识符在我的 UITableViewDataSource 委托(delegate)方法中创建一个单元格。

但是,如果我启动应用程序,则仅显示左侧标签的文本。详细信息标签不可见。

enter image description here我查看了调试器,发现详细标签文本是正确的。右边标签的文字确实是“世界”。但是,标签不可见。 enter image description here

我做了一些研究,有 a similar problem在过去。每当detailLabel的文本设置为nil时,它就不可见

但是在我的示例中,文本不为零,它设置为“详细信息”: enter image description here

如何使正确的标签可见?

最佳答案

if (cell != nil)
{
cell = UITableViewCell(style: UITableViewCellStyle.Subtitle,
reuseIdentifier: reuseIdentifier)
}

将此代码添加到 let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) 这行代码下。

关于ios - UITableView 中的右侧细节不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34370358/

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