如何正确配置UITableView自定义单元格

时间:2018-09-27 17:09:52

标签: ios swift uitableview

我正在尝试为UITableView创建一个自定义单元格,但是无论是将其与情节提要中的标识符一起使用该单元格,还是完全创建自己的单元格,由于某种原因,数据会加载,但滚动时只有一个单元格。一次只显示一个单元格。这是我目前正在尝试的方法:

 let cell = UITableViewCell()
let cellTextLabel = UILabel()
let cellDetailLabel = UILabel()
let imageView = UIImageView()
let phImage = UIImage(named: "CryptiXJustX")

 func configureCustomCell() {
    cell.addSubview(imageView)
    imageView.backgroundColor = .clear
    imageView.translatesAutoresizingMaskIntoConstraints = false
    imageView.widthAnchor.constraint(equalToConstant: 80).isActive = true
    imageView.heightAnchor.constraint(equalToConstant: 80).isActive = true
    imageView.leftAnchor.constraint(equalTo: cell.leftAnchor, constant: 10).isActive = true
    imageView.centerYAnchor.constraint(equalTo: cell.centerYAnchor).isActive = true
    cell.addSubview(cellTextLabel)
    cellTextLabel.translatesAutoresizingMaskIntoConstraints = false
    cellTextLabel.leftAnchor.constraint(equalTo: imageView.rightAnchor, constant: 10).isActive = true
    cellTextLabel.rightAnchor.constraint(equalTo: cell.contentView.rightAnchor, constant: -10).isActive = true
    cellTextLabel.topAnchor.constraint(equalTo: cell.contentView.topAnchor, constant: 10).isActive = true
    cellTextLabel.textColor = .white
    cell.addSubview(cellDetailLabel)
    cellDetailLabel.translatesAutoresizingMaskIntoConstraints = false
    cellDetailLabel.leftAnchor.constraint(equalTo: imageView.rightAnchor, constant: 10).isActive = true
    cellDetailLabel.rightAnchor.constraint(equalTo: cell.contentView.rightAnchor, constant: -10).isActive = true
    cellDetailLabel.topAnchor.constraint(equalTo: cellTextLabel.bottomAnchor, constant: 10).isActive = true
    cellDetailLabel.textColor = .white
    cellDetailLabel.font = cellDetailLabel.font.withSize(12)
    cellDetailLabel.numberOfLines = 3

    cell.backgroundColor = .clear
    cell.textLabel?.textColor = .white
    cell.detailTextLabel?.textColor = .white

    imageView.image = phImage
}

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

     var index = indexPath.row
    if contentData.count > 0 {
    cellTextLabel.text = contentData[index].title
    cellDetailLabel.text = contentData[index].text
        return cell
    } else {
        return cell
    }
}

我在configureCustomCell()ViewDidLoad(),我觉得这与cellforRowAt方法有关,但是我不确定。

override func viewDidLoad() {
    super.viewDidLoad()

     view.backgroundColor = .clear

    configureCustomCell()

    NewsAPI.shared.getData(arr: true) { (success) in
        DispatchQueue.main.async {

            self.contentData = NewsAPI.shared.contentData[0].posts
            self.tableView.reloadData()
        }
    }
    }

谢谢。

2 个答案:

答案 0 :(得分:1)

按如下所示创建自定义UITableViewCell

class CustomCell: UITableViewCell {
}

在控制器viewDidLoad中,您将如下所示注册上述单元格

tableView.registerClass(CustomCell.self, forCellReuseIdentifier: "CustomCell")

答案 1 :(得分:0)

原因是因为您在整个表视图的单个实例单元上使用。这是将数据加载到UITableView中的不正确方法

但是您可以尝试使用代替:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var index = indexPath.row
    if contentData.count > 0 {
        cellTextLabel.text = contentData[index].title
        cellDetailLabel.text = contentData[index].text

        return cell
    } else {
        return cell
    }
}

尝试以这种方式使用:

class MyCell: UITableViewCell {
    let cellTextLabel = UILabel()
    let cellDetailLabel = UILabel()
    let imageView = UIImageView()
    let phImage = UIImage(named: "CryptiXJustX")

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        contentView.addSubview(imageView)
        imageView.backgroundColor = .clear
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.widthAnchor.constraint(equalToConstant: 80).isActive = true
        imageView.heightAnchor.constraint(equalToConstant: 80).isActive = true
        imageView.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 10).isActive = true
        imageView.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
        contentView.addSubview(cellTextLabel)
        cellTextLabel.translatesAutoresizingMaskIntoConstraints = false
        cellTextLabel.leftAnchor.constraint(equalTo: imageView.rightAnchor, constant: 10).isActive = true
        cellTextLabel.rightAnchor.constraint(equalTo: contentView.contentView.rightAnchor, constant: -10).isActive = true
        cellTextLabel.topAnchor.constraint(equalTo: contentView.contentView.topAnchor, constant: 10).isActive = true
        cellTextLabel.textColor = .white
        contentView.addSubview(cellDetailLabel)
        cellDetailLabel.translatesAutoresizingMaskIntoConstraints = false
        cellDetailLabel.leftAnchor.constraint(equalTo: imageView.rightAnchor, constant: 10).isActive = true
        cellDetailLabel.rightAnchor.constraint(equalTo: contentView.contentView.rightAnchor, constant: -10).isActive = true
        cellDetailLabel.topAnchor.constraint(equalTo: cellTextLabel.bottomAnchor, constant: 10).isActive = true
        cellDetailLabel.textColor = .white
        cellDetailLabel.font = cellDetailLabel.font.withSize(12)
        cellDetailLabel.numberOfLines = 3

        contentView.backgroundColor = .clear
        contentView.textLabel?.textColor = .white
        contentView.detailTextLabel?.textColor = .white
    }
}

在您的ViewDidLoad上:

override func viewDidLoad() {
    super.viewDidLoad()
    // if you are not using storyboard, make sure you are registered the cell ID
    self.tableView!.register("CellID", forCellReuseIdentifier: tableReuseIdentifier)
}

最后:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: 'CellID', for: indexPath) as! MyCell

    cell.imageView.image = phImage
    cell.cellTextLabel.text = contentData[indexPath.row].title
    cell.cellDetailLabel.text = contentData[indexPath.row].text

    return cell
}