我创建了一个自定义单元格视图,并在故事板中创建了一个默认图像。但由于某些原因,我的细胞没有出现。我该怎么做才能让我的细胞出现。
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCellWithIdentifier("TableViewCell") as? TableViewCell {
return cell
}
else{
return TableViewCell()
}
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
}
import UIKit
class TableViewCell: UITableViewCell {
@IBOutlet weak var mainImg: UIImageView!
@IBOutlet weak var mainLbl: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
func configureCell(image: UIImage, text: String){
mainImg.image = image
mainLbl.text = "Hello Im Tyree"
}
}
答案 0 :(得分:0)
您错过了在viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
因此,所有的tableView函数代码都不会被执行。
答案 1 :(得分:0)
答案 2 :(得分:0)
尝试此代码,您不必检查具有标识符的单元格是否存在,因为它始终应该。并在索引路径中的行的单元格中设置单元格的属性,而不是在自定义子类中。您还需要在视图中设置委托和数据源加载
viewDidLoad() {
tableView.dataSource = self
tableView.delegate = self
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell : TableViewCell = tableView.dequeueReusableCellWithIdentifier("TableViewCell") as! TableViewCell
cell.mainImg.image = image
mainLbl.text = "Hello im tryee"
return cell
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
,子类应为
class TableViewCell: UITableViewCell {
@IBOutlet weak var mainImg: UIImageView!
@IBOutlet weak var mainLbl: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}