【发布时间】:2017-03-29 02:38:40
【问题描述】:
我的故事板是这样的
我的代码如下
UIViewController
类 DownLoadSoundsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
// MARK: View Controller Properties
let viewName = "DownLoadSoundsViewController"
@IBOutlet weak var visualEffectView: UIVisualEffectView!
@IBOutlet weak var dismissButton: UIButton!
@IBOutlet weak var downloadTableView: UITableView!
// MARK: Properties
var soundPacks = [SoundPack?]() // structure for downloadable sounds
override func viewDidLoad() {
super.viewDidLoad()
downloadTableView.dataSource = self
downloadTableView.delegate = self
downloadTableView.register(DownLoadTableViewCell.self, forCellReuseIdentifier: "cell")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return numberOfSoundPacks
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let method = "tableView.cellForRowAt"
//if (indexPath as NSIndexPath).section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "downloadTableViewCell", for: indexPath) as! DownLoadTableViewCell
cell.backgroundColor = UIColor.green
if soundPacks[(indexPath as NSIndexPath).row]?.price == 0 {
cell.soundPackPriceUILabel.text = "FREE"
} else {
cell.soundPackPriceUILabel.text = String(format: "%.2", (soundPacks[(indexPath as NSIndexPath).row]?.price)!)
}
//cell.textLabel?.text = soundPacks[(indexPath as NSIndexPath).row]?.soundPackTitle
cell.soundPackTitleUILabel.text = soundPacks[(indexPath as NSIndexPath).row]?.soundPackTitle
cell.soundPackAuthorUILabel.text = soundPacks[(indexPath as NSIndexPath).row]?.author
cell.soundPackShortDescription.text = soundPacks[(indexPath as NSIndexPath).row]?.shortDescription
cell.soundPackImage.image = UIImage(named: "Placeholder Icon")
DDLogDebug("\(viewName).\(method): table section \((indexPath as NSIndexPath).section) row \((indexPath as NSIndexPath).row))")
return cell
//}
}
UItableViewCell
类 DownLoadTableViewCell: UITableViewCell {
@IBOutlet weak var soundPackImage: UIImageView!
@IBOutlet weak var soundPackTitleUILabel: UILabel!
@IBOutlet weak var soundPackAuthorUILabel: UILabel!
@IBOutlet weak var soundPackShortDescription: UILabel!
@IBOutlet weak var soundPackPriceUILabel: UILabel!
let gradientLayer = CAGradientLayer()
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
但我得到以下信息;
我确定我做错了一些小事情,但到目前为止还无法弄清楚。查看了许多示例,包括我自己的代码,我之前已经在其中工作过。
除了单元格的数量之外,我的表格视图设置没有被调用。但一切都在;
func tableView(_tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{...}
不工作。
感谢您的帮助。
【问题讨论】:
-
你能打印
numberOfSoundPacks.count吗?我认为这是一个空数组。 -
是的。它是 3。但可能就是这样。它是在从火力基地回电后设置的。所以可能没有及时设置。会尽力让你知道。
-
好的!从 Firebase 获取数据后,您是否重新加载了 tableView?
downloadTableView.reloadData() -
你太棒了。就是这样。需要添加 self.downloadTableView.reloadData() 现在,就像一个魅力。呃。盯着这段代码太久了。
-
是的!我添加了一个答案,请接受它以帮助其他人。
标签: uitableview prototype cell