RoxyS
RoxyS

Reputation: 3

UITableView does not show the cells.

I have implemented in my project with CocoaPods RESideMenu and I created a view that contains the table then. My problem is that the table does not make me see the cells, xcode me no mistakes and then not know where I'm wrong. Can anyone help? I enter the code:

import UIKit

class RightMenuViewController: UIViewController, UITableViewDataSource,    UITableViewDelegate {

var tableView = UITableView()
var titles = ["Home","Profilo","Notizie","Impostazioni"]
//var images = ["IconHome","IconCalendar","IconProfile","IconSettings","IconEmpty"]

init(frame: CGRect, dialStrings: [String]) {
    super.init(nibName: nil, bundle: nil)
    self.tableView = UITableView(frame: CGRectMake(0, (frame.size.height - 54 * 2) / 2.0, frame.size.width, 54 * 2), style:UITableViewStyle.Plain)
}


override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.autoresizingMask = UIViewAutoresizing.FlexibleBottomMargin | UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleWidth
    self.tableView.contentInset = UIEdgeInsetsMake(100.0, 0, 0, 0)
        //contentInset = UIEdgeInsetsMake(64.0, 0, 0, 0)
    self.tableView.delegate = self
    self.tableView.dataSource = self
    self.tableView.opaque = false
    self.tableView.bounces = false
    self.tableView.backgroundColor = UIColor.redColor()
    self.tableView.separatorStyle = .None
    self.tableView.backgroundView = nil //UIImageView(image: UIImage(named: "BackMenu"))
    self.tableView.scrollsToTop = false
    self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "CELL")
    view.addSubview(self.tableView)


}



required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source
 func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 54.0
}


 func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1
}

 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return titles.count
}

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as? UITableViewCell

    if (cell == nil){
        cell = UITableViewCell()
        cell!.backgroundColor = UIColor.blueColor()
        cell!.textLabel.font = UIFont(name: "HelveticaNeue", size: 21)
        cell!.textLabel.textAlignment = NSTextAlignment.Right
        cell!.textLabel.textColor = UIColor.whiteColor()
        cell!.textLabel.highlightedTextColor = UIColor.lightGrayColor()
        cell!.selectedBackgroundView = UIView.alloc()

    }
        return cell!
}

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main",bundle: nil)
    var salViewController: UIViewController
    switch (indexPath.row){
    case 0:
        salViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("Home") as UIViewController
        break
    case 1:
        salViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("Profilo") as UIViewController
        break
    case 2:
        salViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("Notizie") as UIViewController
        break
    case 3:
        salViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("Impostazioni") as UIViewController
        break
    default:
        break

    }

}

}

Upvotes: 0

Views: 7005

Answers (3)

Jageen
Jageen

Reputation: 6365

Problem is in the below line.

self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "CELL")

This will create a cell using identifier "Cell" in reuse queue so that when you write code like below

var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as? UITableViewCell

    if (cell == nil){
        cell = UITableViewCell()
        cell!.backgroundColor = UIColor.blueColor()
        cell!.textLabel.font = UIFont(name: "HelveticaNeue", size: 21)
        cell!.textLabel.textAlignment = NSTextAlignment.Right
        cell!.textLabel.textColor = UIColor.whiteColor()
        cell!.textLabel.highlightedTextColor = UIColor.lightGrayColor()
        cell!.selectedBackgroundView = UIView.alloc()    
    }

cell will never nil

Remove that line and all works perfect

Reference
enter image description here

Upvotes: 3

Salvatore Sepe
Salvatore Sepe

Reputation: 30

You must enter this line of code will not see otherwise never your Array Titles:

cell!.textLabel.text = titles[indexPath.row]

once inserted properly will see the menu.

Upvotes: 0

Sean
Sean

Reputation: 370

In func tableView(...), you never assign any data to your cells. It makes sense that they're blank.

Upvotes: 0

Related Questions