Ios TableViewCell仅返回零
我无法确定tableViewCell为什么一直返回零。这是我几乎可以肯定的导致问题的代码块Ios TableViewCell仅返回零,ios,swift,Ios,Swift,我无法确定tableViewCell为什么一直返回零。这是我几乎可以肯定的导致问题的代码块 func tableView(tableView: UITableView, cellForRowAt indexPath: NSIndexPath) -> UITableViewCell { var cell = tableView.dequeueReusableCell(withIdentifier: "CollectionCell", for: indexPath as IndexPa
func tableView(tableView: UITableView, cellForRowAt indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "CollectionCell", for: indexPath as IndexPath) as? CollectionViewCell
if cell == nil {
var nib = Bundle.main.loadNibNamed("CollectionCell", owner: self, options: nil)
cell = nib![0] as? CollectionViewCell
}
if cell == nil {
cell = tableView.dequeueReusableCell(withIdentifier: "CollectionCell") as! CollectionViewCell
}
// ...
}
这是请求的整个CollectionViewCell文件
class CollectionViewCell: UITableViewCell {
@IBOutlet weak var txtNameCollection: UITextField!
@IBOutlet weak var btnGoToCollection: UIButton!
@IBOutlet weak var imgShelf: UIImageView!
@IBOutlet weak var btnNameCollection: UIButton!
@IBOutlet weak var btnEditCollectionName: UIButton!
@IBOutlet weak var imgNameCollection: UIImageView!
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
}
}您是否已在tableView.register中注册了单元格(uU2;:forCellReuseIdentifier:)?还有,什么是CollectionViewCell?它不是
UICollectionViewCell
的一个子类吗?如果条件错误,这两个。他们不应该在那里。在这个方法中,您必须使用的唯一一件事是tableView.dequeueReusableCell()
。所以你的第一行是完全好的。您需要找出为什么在此之后单元格为nil
。1。为什么在表视图中有名为“CollectionCell”的东西?2.为什么你要使用非常过时的SWIFT 2签名,用于<代码> CyfFoRoAT< <代码>?你应该共享CollectionViewCell.if的代码和XIB,你应该使用XIB,你应该考虑使用故事板和。正如@nguyen hoan已经说过的,你需要提供
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib(nibName: "CollectionCell", bundle: nil), forCellReuseIdentifier: "CollectionCell")
}
func tableView(tableView: UITableView, cellForRowAt indexPath: NSIndexPath) -> UITableViewCell {
//var cell = tableView.dequeueReusableCell(withIdentifier: "CollectionCell", for: indexPath as IndexPath) as? CollectionViewCell
var cell = tableView.dequeueReusableCell(withIdentifier: "CollectionCell" ) as? CollectionViewCell
cell?.txtNameCollection.text = "Text"
return cell
You have to delete this because you use Xib File. And you dont do prototype cell on storyboard. There is no need. Just create a tableView with name is tableView
// if cell == nil {
// cell = tableView.dequeueReusableCell(withIdentifier: "CollectionCell") as! CollectionViewCell
// }
// if cell == nil {
// var nib = Bundle.main.loadNibNamed("CollectionCell", owner: self, options: nil)
// cell = nib![0] as? CollectionViewCell
// }
}