iOS表格视图自定义单元格

时间:2017-09-15 01:06:22

标签: ios swift uitableview custom-cell

我的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!

我仔细检查了所有内容是否正确连接所以请帮我弄清楚我遇到这个问题的原因。

1 个答案:

答案 0 :(得分:0)

您已经使用了UITableViewController,因此您需要将部分的数量返回为1.因此,请更改以下代码:

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

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