【发布时间】:2016-03-26 00:47:14
【问题描述】:
我有一个UIViewController,其中嵌入了UITableView。因为我不想让UIViewController 变得太重,所以我将UITableViewDataSource 和UITableViewDelegate 分开了
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 中创建了一个原型单元格,其标识符为
MyCellIdentifier
我使用此标识符在我的 UITableViewDataSource 委托方法中创建一个单元格。
但是,如果我启动应用程序,则只显示左侧标签的文本。详细标签不可见。
我查看了调试器并注意到 detailLabel 文本是正确的。正确标签的文字是真正的“世界”。但是,标签是不可见的。
我做了一些研究,过去有a similar problem。每当 detailLabel 的文本设置为 nil 时,它是不可见的
但是在我的示例中,文本不是 nil,而是设置为“详细信息”:
如何使正确的标签可见?
【问题讨论】:
-
你的约束设置是否正确?
-
您是否已经尝试过我关于约束的建议?查看您的屏幕截图,您可能在那里设置了固定宽度,这没有帮助。
-
添加解决了这个问题。谢谢。
-
请上传您的约束的屏幕截图
-
在我的情况下,我在某些时候删除了约束。在没有任何限制的情况下,屏幕被调整了大小并且标签被隐藏了。
标签: ios swift uitableview uiviewcontroller storyboard