带有cellviewwithtag的SIGBART

时间:2018-04-01 15:55:00

标签: ios swift uitableview

我想知道为什么我在这段代码上遇到SIGBART错误。我为每个图像和按钮分配了一个标签,但我仍然收到错误。有谁知道为什么会这样?

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

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cat") as UITableViewCell!

    let image = cell?.viewWithTag(1) as! UIImageView //the error is here
    image.image = UIImage(named: "CatIcon")

    let button = cell?.viewWithTag(2) as! UIButton
    button.setTitle("Cat: \(CatView.catsnum[indexPath.row])", for: .normal)
    button.titleLabel?.font = UIFont(name: Font, size: 20)
    button.setTitleColor(.white, for: .normal)
    button.addTarget(self, action: #selector(CatLink), for: .touchUpInside)
    button.backgroundColor = .black
    button.backgroundColor?.withAlphaComponent(0.5)

    return cell!
}

1 个答案:

答案 0 :(得分:0)

您必须使用单元格注册tableView

tableView.register(customClassName.self, forCellReuseIdentifier: "Cat")

或者使用xib

tableView.register(UINib(nibName: "customClassName", bundle: nil), forCellReuseIdentifier:"Cat")