import UIKit
class NewOrdersViewControllers: UIViewController,UITableViewDelegate,UITableViewDataSource {
var items = ["Chilli and Lemon"]
@IBOutlet var tableView:UITableView!
@IBOutlet weak var lblRestaurantNames: UILabel!
@IBOutlet weak var mapView: UIButton!
var cellIdentifier = "cell"
init() {
super.init(nibName : "NewOrdersViewControllers", bundle:nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
let nib = UINib(nibName: "tableCell", bundle: nil)
self.tableView.registerNib(nib, forCellReuseIdentifier: cellIdentifier)
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: cellIdentifier)
tableView.delegate = self
tableView.dataSource = self
// Do any additional setup after loading the view.
}
@IBAction func mapPush(sender: AnyObject) {
let mapVC = MapViewController()
self.navigationController?.pushViewController(mapVC, animated: true)
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:tableCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! tableCell
//cell.textLabel?.text = self.items[indexPath.row] as! String
cell.RestaurantLbl.text = self.items[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
print("You selected cell #\(indexPath.row)!")
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 100
}
我使用 xib 完成了它,它显示了以下错误。
Could not cast value of type 'UITableViewCell' (0x1fe82bc)
最佳答案
在您的 viewDidLoad()
方法中,您为相同的 reuseIdentifier
注册了 2 个不同的单元格类型,因此最后一个 (UITableViewCell
) 生效.这就是为什么在 tableView:cellForRowAtIndexPath:
中出队的单元格也是 UITableViewCell
类,可能不会转换为您的自定义单元格类。
尝试删除该行:
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: cellIdentifier)
它应该可以解决您的问题并让 tableView
出列正确的单元格。
关于ios - 我无法在 TableView 内加载 TableView 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34493111/