Ios Swift自定义UITableViewCell不显示来自阵列的数据

Ios Swift自定义UITableViewCell不显示来自阵列的数据,ios,swift,uitableview,xcode7,Ios,Swift,Uitableview,Xcode7,我不熟悉Swift和iOS开发。我正在尝试创建自定义UITableViewCell。我已经在UIViewController中的UITableView顶部的主情节提要中创建了单元格。当我加载一个默认单元格时,我能够用数据填充它。但是,现在我使用的是自定义单元格,因此无法在表中显示任何数据。我已经阅读了网上发布的各种教程和问题,但我不明白为什么它不起作用。任何帮助都将不胜感激 以下是我为tableview所在的UIViewController编写的代码 import UIKit class Vi

我不熟悉Swift和iOS开发。我正在尝试创建自定义UITableViewCell。我已经在UIViewController中的UITableView顶部的主情节提要中创建了单元格。当我加载一个默认单元格时,我能够用数据填充它。但是,现在我使用的是自定义单元格,因此无法在表中显示任何数据。我已经阅读了网上发布的各种教程和问题,但我不明白为什么它不起作用。任何帮助都将不胜感激

以下是我为tableview所在的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类:

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

我想,请在viewDidLoad方法中写下这一点,然后再试一次

override func viewDidLoad() {
    super.viewDidLoad()

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

执行以下操作:

  • 检查单元格和类名的输入错误
  • 检查UITableview的委托和数据源是否正确绑定
  • 如果您将xib用于TableViewCell,请在VDL中注册您的xib
  • 注意: 1.在
    as
    customCell
    之间留出一个空格,就像这样
    as!自定义单元格
    。如果你声明,iOS会更好 像这样的变量单元格
    让cell:CustomCell=..
    因为在这种情况下,x-code知道您的变量类型。[有时可能不会产生冲突,但它不是一种良好的编码实践]

    2.可能会更改您的tableview插座名称
    @IBOutlet var tableview:UITableView到任何其他名称,因为
    tableView
    已在iOs使用的内部版本名称中。如果使用相同的变量,则它会覆盖并导致x代码冲突

    编辑

    而不是将self.tableView.dequeueReusableCellWithIdentifier(“cell”,forIndexPath:indexPath)作为!CustomCell
    将self从行中删除,因为您的
    tableView
    对象没有排队,这里您只需要将
    cellForRow
    方法的参数排队

    使用以下代码:

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

    使用下面的演示图像从
    情节提要
    中绑定您的
    代理
    数据源
    ,我已经添加了输出,请检查

    演示图像

    项目的输出:


    执行以下操作:

  • 检查单元格和类名的输入错误
  • 检查UITableview的委托和数据源是否正确绑定
  • 如果您将xib用于TableViewCell,请在VDL中注册您的xib
  • 注意: 1.在
    as
    customCell
    之间留出一个空格,就像这样
    as!自定义单元格
    。如果你声明,iOS会更好 像这样的变量单元格
    让cell:CustomCell=..
    因为在这种情况下,x-code知道您的变量类型。[有时可能不会产生冲突,但它不是一种良好的编码实践]

    2.可能会更改您的tableview插座名称
    @IBOutlet var tableview:UITableView到任何其他名称,因为
    tableView
    已在iOs使用的内部版本名称中。如果使用相同的变量,则它会覆盖并导致x代码冲突

    编辑

    而不是将self.tableView.dequeueReusableCellWithIdentifier(“cell”,forIndexPath:indexPath)作为!CustomCell
    将self从行中删除,因为您的
    tableView
    对象没有排队,这里您只需要将
    cellForRow
    方法的参数排队

    使用以下代码:

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

    使用下面的演示图像从
    情节提要
    中绑定您的
    代理
    数据源
    ,我已经添加了输出,请检查

    演示图像

    项目的输出:


    确保已设置单元格标识符


    确保已设置单元格标识符


    我也有这个问题,上面的问题对我来说并不完全适用。我还需要做的是确保数据源和代理出口连接到视图控制器(并且我的视图控制器类包括UITableViewDelegate和UITableViewDataSource)。检查此项,以防上述问题无法解决

    我也有这个问题,上面的问题对我来说并不完全适用。我还需要做的是确保数据源和代理出口连接到视图控制器(并且我的视图控制器类包括UITableViewDelegate和UITableViewDataSource)。检查此项,以防上述问题无法解决

    您需要在viewDidLoad或Stroyboard中自行设置tableView委托和数据源

    override func viewDidLoad() {
      super.viewDidLoad()
    
      tableView.delegate = self
      tableView.dataSource = self
    }
    

    您需要在viewDidLoad或Stroyboard中自行设置tableView委托和数据源

    override func viewDidLoad() {
      super.viewDidLoad()
    
      tableView.delegate = self
      tableView.dataSource = self
    }
    

    您可以共享CustomCell的代码吗?在序列图像板中,检查代理和数据源是否与ViewController连接。我检查引用oulet是否与ViewController连接,但数据源和代理没有连接。是吗?@ThinhLe您还需要将
    datasource
    delegate
    与您的
    ViewController
    连接起来。完成后,它会显示错误。然后我四处寻找这个问题。他们说我有这样一行:let cell=self.tableView.dequeueReusableCellWithIdentifier(“cell”,forIndexPath:indexPath)为!CustomCell,因此我不需要在override func viewDidLoad()中定义{super.viewDidLoad()//加载视图后,通常从nib执行任何其他设置。}您可以共享CustomCell的代码吗?在序列图像板中,检查代理和数据源是否与ViewController连接。我检查引用oulet是否与ViewController连接,但数据源和代理没有连接。是吗?@你需要这么做