Swift tableview 数据填充
Swift tableview data population
在 swift 中只制作一个简单的 Tableview,该 tableview 根本不填充任何内容。正在填充图像。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "cellIdentifier";
var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? UITableViewCell;
if !(cell != nil){
cell = UITableViewCell(style: UITableViewCellStyle.Subtitle,
reuseIdentifier: cellIdentifier)
}
if(indexPath.row==0){
cell!.textLabel.text = "POG Validation"
cell!.imageView.image = UIImage(named: "myImg")
}
return cell;
cell!.textLabel
的帧是(0,0,0,0)。并且没有数据被填充。
(lldb) po cell!.textLabel;
<UITableViewLabel: 0x7ce6c510; frame = (0 0; 0 0); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7ce6c5d0>>
一旦我修复了你的编译错误,你的代码就可以正常工作了:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "cellIdentifier";
var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? UITableViewCell;
if !(cell != nil){
cell = UITableViewCell(style: UITableViewCellStyle.Subtitle,
reuseIdentifier: cellIdentifier)
}
if(indexPath.row==0){
// your forgot the '?'s
cell!.textLabel?.text = "POG Validation"
cell!.imageView?.image = UIImage(named: "myImg")
}
return cell!; // you forgot the '!'
}
我会这样写:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "cellIdentifier";
let dequedCell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? UITableViewCell
let cell = dequedCell ?? UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: cellIdentifier) as UITableViewCell
if(indexPath.row==0){
cell.textLabel?.text = "POG Validation"
cell.imageView?.image = UIImage(named: "myImg")
}
return cell;
}
在 swift 中只制作一个简单的 Tableview,该 tableview 根本不填充任何内容。正在填充图像。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "cellIdentifier";
var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? UITableViewCell;
if !(cell != nil){
cell = UITableViewCell(style: UITableViewCellStyle.Subtitle,
reuseIdentifier: cellIdentifier)
}
if(indexPath.row==0){
cell!.textLabel.text = "POG Validation"
cell!.imageView.image = UIImage(named: "myImg")
}
return cell;
cell!.textLabel
的帧是(0,0,0,0)。并且没有数据被填充。
(lldb) po cell!.textLabel;
<UITableViewLabel: 0x7ce6c510; frame = (0 0; 0 0); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7ce6c5d0>>
一旦我修复了你的编译错误,你的代码就可以正常工作了:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "cellIdentifier";
var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? UITableViewCell;
if !(cell != nil){
cell = UITableViewCell(style: UITableViewCellStyle.Subtitle,
reuseIdentifier: cellIdentifier)
}
if(indexPath.row==0){
// your forgot the '?'s
cell!.textLabel?.text = "POG Validation"
cell!.imageView?.image = UIImage(named: "myImg")
}
return cell!; // you forgot the '!'
}
我会这样写:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "cellIdentifier";
let dequedCell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? UITableViewCell
let cell = dequedCell ?? UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: cellIdentifier) as UITableViewCell
if(indexPath.row==0){
cell.textLabel?.text = "POG Validation"
cell.imageView?.image = UIImage(named: "myImg")
}
return cell;
}