表格视图单元格未显示

时间:2016-03-17 04:17:06

标签: ios uitableview

我创建了一个自定义单元格视图,并在故事板中创建了一个默认图像。但由于某些原因,我的细胞没有出现。我该怎么做才能让我的细胞出现。

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"
}

}

3 个答案:

答案 0 :(得分:0)

您错过了在viewDidLoad()

中的设置
tableView.dataSource = self
tableView.delegate = self

因此,所有的tableView函数代码都不会被执行。

答案 1 :(得分:0)

  1. 检查委托,数据源与您的tableview相关联 XIB。
  2. func configureCell 没有函数调用。

答案 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
}
相关问题