Swift 自定义 UITableViewCell 不显示数组中的数据

Swift Custom UITableViewCell not displaying data from array

我是 Swift 的新手,一般来说 iOS 开发。我正在尝试创建自定义 UITableViewCell。我在 UIViewController 内的 UITableView 之上的主故事板中创建了单元格。当我加载其中一个默认单元格时,我能够用数据填充它。但是,现在我正在使用自定义单元格,我无法让任何数据出现在 table 中。我浏览了互联网上发布的各种教程和问题,但我无法弄清楚为什么它不起作用。任何帮助将不胜感激。

这是 table 视图所在的 UIViewController 的代码。

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet var tableView: UITableView!
var name = ["Thinh", "Nhut", "Truong", "Tuyet", "Hoai", "Hoang", "NSHipster", "iOS Developer Tips", "Jameson Quave", "Natasha The Robot", "Coding Explorer", "That Thing In Swift", "Andrew Bancroft", "iAchieved.it", "Airspeed Velocity"]
var ngaysinh = ["10/10/1991", "10/10/1991", "10/10/1991", "10/10/1991", "10/10/1991", "10/10/1991", "10/10/1991", "10/10/1991", "10/10/1991", "10/10/1991", "10/10/1991", "10/10/1991", "10/10/1991", "10/10/1991", "10/10/1991"]
var namsinh = ["1991", "1991", "1991", "1991", "1991", "1991", "1991", "1991", "1991", "1991", "1991", "1991", "1991", "1991", "1991"]


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return name.count
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)


public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell

    cell.hoten.text = name [indexPath.row]
    cell.ngaysinh.text = ngaysinh [indexPath.row]
    cell.namsinh.text = namsinh [indexPath.row]

    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)

    let row = indexPath.row
    print(name[row])
    print(ngaysinh[row])
    print(namsinh[row])
}


}

这是 CustomCell Class:

import UIKit

class CustomCell: UITableViewCell {

@IBOutlet weak var hoten: UILabel!
@IBOutlet weak var ngaysinh: UILabel!
@IBOutlet weak var namsinh: UILabel!
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
}

}

我想,请把这个写在你的viewDidLoad方法中试试,

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self
    // Do any additional setup after loading the view.
}

执行以下操作:

  1. 检查单元格和 class 名称的拼写错误
  2. 检查UITableview的delegate和datasource是否正确绑定
  3. 如果您将 xib 用于 TableViewCell,则在 VDL 中注册您的 xib

注: 1. 在ascustomCell之间做一个space,像这样as! CustomCell。如果你声明,它对 iOS 更好 像这样的可变单元格 let cell : CustomCell = .......... 因为在这种情况下 x-code 知道你的变量类型。 [可能有时它不会产生冲突,但它不是编码的好习惯]。

2 。可能将您的 tableview 出口名称 @IBOutlet var tableView: UITableView! 更改为任何其他名称,因为 tableView 已经在 iOs 所采用的构建名称中,如果您使用相同的变量,那么它会覆盖并且导致了 x-code.

的冲突

edit

而不是 self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as!CustomCell 从行中删除 self 因为您没有使 tableView 对象出列,在这里您只需要使 cellForRow 方法的参数出列。

使用下面的代码:

let cell : CustomCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell

Solution

使用下面的演示图像从 storyboard 绑定您的 delegatesdatasource,我在执行此操作后添加了输出,请检查。

演示图片

项目输出:

确保您已设置单元格标识符

我也遇到了这个问题,上面的内容对我来说并没有完全解决。我还需要做的是确保将数据源和委托出口连接到视图控制器(并且我的视图控制器 class 包括 UITableViewDelegate 和 UITableViewDataSource)。如果上面没有为您解决问题,请检查此项。

您需要在 viewDidLoad 或 Storyboard 中自行创建 tableView Delegate 和 Datasource。

override func viewDidLoad() {
  super.viewDidLoad()

  tableView.delegate = self
  tableView.dataSource = self
}