[英]table view cell not appearing
I have created a custom cell view and I made a default image in the story board. 我创建了一个自定义单元格视图,并在故事板上制作了默认图像。 But for some reason my cell is not appearing.
但是由于某种原因,我的细胞没有出现。 What should I do to make my cell appear.
我应该怎么做才能使我的细胞出现。
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"
}
} }
You missed setting this in your viewDidLoad()
: 您错过了在
viewDidLoad()
设置此设置:
tableView.dataSource = self
tableView.delegate = self
So all of your tableView function code will never be excuted. 因此,您所有的tableView函数代码都不会被剥夺。
Try this code, you dont neet to check that a cell with the identifier exists because it ALWAYS should. 尝试使用此代码,您无需检查具有标识符的单元格是否存在,因为它始终应存在。 And set the property's of the cell in the cell for row at index path, not in the custom subclass.
并在索引路径(而不是自定义子类)中为行设置单元格中单元格的属性。 Also you need to set the delegate and datasource in the view did load
另外,您需要在加载后的视图中设置委托和数据源
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
}
and the subclass should be 并且子类应该是
class TableViewCell: UITableViewCell {
@IBOutlet weak var mainImg: UIImageView!
@IBOutlet weak var mainLbl: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.