我在學習故事板教程,主要介紹Tab Bar Controller,Navigation Controller和Table View Controller的使用。表格視圖控制器嵌入在導航控制器中。爲什麼我的Table View Cell無法顯示?
但是,當我運行這個應用程序時,表格視圖單元格不能被看到。我仔細檢查了我的代碼,但仍然找不到原因。這裏有兩個故事板的法師。 Document Outline,click here. View Controller Scene, here.
ViewController.swift中的代碼在這裏。
import UIKit
import Foundation
struct Player
{
var name:String?
var game:String?
var rating:Int
init(name:String?, game:String?,rating:Int)
{
self.name = name
self.game = game
self.rating = rating
}
}
let playersData = [
Player(name: "Bill Evans", game: "TTT", rating: 4),
Player(name: "OD", game: "Spin the Bottle", rating: 5),
Player(name: "DB", game: "Texas Holder", rating: 2)]
class PlayersViewController: UITableViewController {
var players:[Player] = playersData
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return players.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
//This method will check to see if there is an existing cell that can be recycled.If not, it will automatically allocate a prototype cell and return it to you.
let cell = tableView.dequeueReusableCellWithIdentifier("PlayerCell", forIndexPath: indexPath)
let player = players[indexPath.row]as Player
cell.textLabel?.text = player.name
cell.detailTextLabel?.text = player.game
return cell
}
}
你有沒有設置tableview數據源和委託正確?選擇你的視圖控制器,並將表的委託和數據源設置到您的視圖控制器,並重新運行:) –
呃,我檢查了tableview數據源和委託。問題出在身份檢查器。自定義類應該是「PlayersViewController」,而不是「UITableViewController」。謝謝,無論如何 – Magenta