【问题标题】:Issue with iOS table view custom celliOS表格视图自定义单元格的问题
【发布时间】:2017-09-15 01:06:22
【问题描述】:

我的UITableView 自定义单元格有问题。

这是 UITableView 代码:

var contactName = ["Papa", "Mummy", "Ajay", "Deepak", "My"]
var phoneNumber = ["657464567", "354636346", "5436346", "6345634643", "5645634656"]

// MARK: - Table view data source
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return contactName.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! myTableViewCell
    cell.name.text = contactName[indexPath.row]
    return cell
}

这是我的自定义单元格代码:

@IBOutlet weak var name: UILabel!

我仔细检查了所有东西都连接正确,所以请帮助我找出我遇到这个问题的原因。

【问题讨论】:

  • 您是否遇到任何错误?如果是,你能发布那个错误吗
  • 错误不只是显示它在设备或模拟器中显示空表
  • 你为tableview设置数据源了吗?

标签: ios swift uitableview custom-cell


【解决方案1】:

您已经使用了 UITableViewController,因此您需要将节数返回为 1。所以更改如下代码:

 override func numberOfSections(in tableView: UITableView) -> Int {
        return 0
    }

 override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

【讨论】:

  • 我已经删除了这个函数,所以它的默认值为 1,而且我总是这样写,顺便说一下,我也试过这个是(在你建议的上面)我不认为这是一个问题
  • 那么你有没有在日志中遇到任何崩溃。请张贴那个