UITableView不显示单元格。

UITableView不显示单元格。

问题描述:

我已经在CocoaPods RESideMenu的项目中实现了,然后创建了一个包含表格的视图。我的问题是,桌子不让我看到细胞,xcode我没有错误,然后不知道我错了。谁能帮忙?我输入代码:UITableView不显示单元格。

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 

    } 

} 

} 
+0

因此,在numberOfRows,NSLog'titles.count'中。 – 2014-12-13 02:55:27

+0

检查故事板是由故事板呈现的TableViewController吗?您的自定义RightMenuViewController是Identity Inspector Alt + Cmd + 3上的类吗? – 2014-12-13 03:33:01

您必须进入这一行的代码将不会看到一些本来不是你数组标题:

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

一旦插入不当会看到菜单。

func tableView(...)中,您从未将任何数据分配给您的单元格。这是有道理的,他们是空白的。

问题在下面的行中。

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

这将在复用队列使用标识符“小区”创建一个小区,这样,当您编写代码象下面

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()  
    } 

细胞永远不会零

删除该行并且所有作品都完美

Reference
enter image description here

+0

我删除了部分代码,但是,并没有让我看到单元格的内容,所以显示上面显示的数组。我在想这是图书馆RESideMenu的一个错误。 – RoxyS 2014-12-13 20:36:34

+0

@RoxyS RESideMenu中可能没有问题,我调试代码并确定该问题,因为您使用“registerClass”。 – Jageen 2014-12-14 03:16:39

+0

@RoxyS你有没有一天,上面的代码不帮你做细胞?如果是这种情况,我必须删除我的答案, – Jageen 2014-12-14 03:32:55