所以我试图在动态TableView中创建UILabel,并设置子类UITableViewCell,但是当我构建我的项目时,UILabel不会显示任何文本。
GamesFeedCell
import UIKit
class GamesFeedCell: UITableViewCell {
@IBOutlet weak var game1Name: UILabel!
@IBOutlet weak var game2Name: UILabel!
@IBOutlet weak var score1: UILabel!
@IBOutlet weak var score2: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization codea
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
GamesFeedViewController
import UIKit
class GamesFeedViewController: UIViewController, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerNib(UINib(nibName: "GamesFeedCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: "gamesFeedCell")
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@available(iOS 2.0, *)
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 8
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: GamesFeedCell! = tableView.dequeueReusableCellWithIdentifier("gamesFeedCell") as! GamesFeedCell
if (cell == nil) {
cell = GamesFeedCell()
}
(cell as GamesFeedCell).game1Name.text = "game1"
(cell as GamesFeedCell).game2Name.text = "game2"
(cell as GamesFeedCell).score1.text = "score1"
(cell as GamesFeedCell).score2.text = "score2"
return cell!
}
}
答案 0 :(得分:0)
我会验证两件事:
dequeueReusableCellWithIdentifier
实际上正在返回GamesFeedCell
的实例 - 如果没有,请确保该标识符与Interface Builder中的单元格相匹配UILabel
的IBOutlets已经连接起来。我也不认为此代码是必要的,并且可能会导致错误,因为如果cell
恰好是nil
,则会init
' d通常,因此您在Interface Builder的Dynamic Prototype中设置的任何内容都不会出现。我的建议:删除此代码(这可能会暴露dequeueReusableCellWithIdentifier
实际上没有出现任何错误的错误,因为标识符是错误的。
if (cell == nil) {
cell = GamesFeedCell()
}