UITableView中的右侧细节不可见

时间:2015-12-19 11:47:23

标签: ios swift uitableview uiviewcontroller storyboard

我有一个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


}

在我的故事板中,我使用标识符

UITableView内创建了一个原型单元格
  

MyCellIdentifier

我使用此标识符在UITableViewDataSource委托方法中创建单元格。

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

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

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

但是在我的示例中,文本不是nil,它设置为“Detail”: enter image description here

如何才能看到正确的标签?

0 个答案:

没有答案