我是 Swift 和 iOS 开发的新手。我正在尝试创建一个自定义 UITableViewCell。我已经在 UIViewController 内的 UITableView 顶部的主情节提要中创建了单元格。当我加载默认单元格之一时,我能够用数据填充它。但是,现在我使用自定义单元格,我无法让任何数据显示在表中。我已经浏览了互联网上发布的各种教程和问题,但我不明白为什么它不起作用。任何帮助,将不胜感激。
这是我的表视图所在的 UIViewController 的代码。
import UIKit
class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tblView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//self.tblView.registerClass(UITableViewCell.self, forCellReuseIdentifier : "Cell")
self.tblView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier : "Cell")
tblView!.delegate = self
tblView!.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataMgr.data.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell : CustomTableViewCell = self.tblView.dequeueReusableCellWithIdentifier("Cell", forIndexPath : indexPath) as! CustomTableViewCell
var values = dataMgr.data[indexPath.row]
cell.newTotalLabel?.text = "\(values.newTotal)"
cell.winLoseValueLabel?.text = "\(values.newTotal - values.currentTotal)"
cell.dateLabel?.text = "5/17/2015"
return cell
}
}
我已经逐步完成了为单元格变量赋值的程序。变量“值”正在填充数据,但是当跨过赋值行到单元格变量时,我发现它们从未被赋值。它们全部保持为零。
当您在故事板中创建自定义单元格时,不要注册类(或其他任何内容)。只需确保在故事板中为单元格提供与您传递到的相同标识符dequeueReusableCellWithIdentifier:forIndexPath:
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)