0

I have a UIViewController in which I've embedded UITableView. Because I don't want the UIViewController to get too heavy I separated the UITableViewDataSource and 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


}

In my Storyboard, I've created a prototype cell within the UITableView with the identifier

MyCellIdentifier

I use this identifier to create a cell in my UITableViewDataSource delegate method.

However, if I start the app, only the text of the left label is displayed. The detail label is not visible.

enter image description here I looked into the debugger and noticed that detailLabel text is correct. The text of the right label is really "world". However, the label is not visible. enter image description here

I've done a little bit of research and there has been a similar problem in the past. Whenever the text of the detailLabel was set to nil, it was not visible

However in my example, the text is not nil, it is set to "Detail": enter image description here

How can I make the right label visible?

Community
  • 1
  • 1
productioncoder
  • 4,225
  • 2
  • 39
  • 65

1 Answers1

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

add this code under let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) this line of your code.

Akshay Patel
  • 157
  • 5
  • It will never come inside `if` as it is dequeuing from the storyboard itself. So `let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)` will never produce a nil – Rajan Maheshwari Dec 19 '15 at 15:26