UITableView不显示单元格。

时间:2014-12-13 02:46:12

标签: uitableview swift xcode6.1

我已经在我的项目中使用CocoaPods RESideMenu实现了,然后我创建了一个包含该表的视图。我的问题是桌子不会让我看到细胞,xcode我没有错误然后不知道我错在哪里。有人可以帮忙吗?我输入代码:

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

    }

}

}

3 个答案:

答案 0 :(得分:3)

问题出在以下行中。

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

这将使用标识符" 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

答案 1 :(得分:0)

func tableView(...)中,您永远不会将任何数据分配给您的单元格。它们是空白的。

答案 2 :(得分:0)

你必须输入这行代码才会看不到你的数组标题:

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

正确插入后会看到菜单。