UITableViewCell左侧的图像消失

时间:2018-02-28 11:22:08

标签: ios swift uitableview

我正在以编程方式设置简单的表视图。一切都没问题,除非我没有将图像设置为单元格的imageView并且只将backgroundColor选项设置为UIColor.black。它只是消失了,也许有人知道我的问题在这里?

    import UIKit

    class FriendRequestsController: UITableViewController {

    static let cellId = "cellId"
    static let headerId = "headerId"

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.title = "Friend Requests"

        tableView.separatorColor = UIColor.rgb(229, green: 231, blue: 235)
        tableView.sectionHeaderHeight = 26

        tableView.register(FriendRequestCell.self, forCellReuseIdentifier: FriendRequestsController.cellId)
        tableView.register(RequestHeader.self, forHeaderFooterViewReuseIdentifier: FriendRequestsController.headerId)
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        cell.backgroundColor = UIColor.blue
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell =  tableView.dequeueReusableCell(withIdentifier: FriendRequestsController.cellId)!

      //cell.imageView?.image = UIImage(named:"gandhi_profile")

        cell.imageView?.backgroundColor = UIColor.black

      return cell
    }

       override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: FriendRequestsController.headerId) as! RequestHeader

        if section == 0 {
            header.nameLabel.text = "FRIEND REQUESTS"
        } else {
            header.nameLabel.text = "PEOPLE YOU MAY KNOW"
        }

        return header
    }

     }

    class RequestHeader: UITableViewHeaderFooterView {

    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        setupViews()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    let nameLabel: UILabel = {
        let label = UILabel()
        label.text = "FRIEND REQUESTS"
        label.font = UIFont.systemFont(ofSize: 10)
        label.textColor = UIColor(white: 0.4, alpha: 1)
        return label
    }()

    let bottomBorderView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.rgb(229, green: 231, blue: 235)
        return view
    }()

    func setupViews() {
        addSubview(nameLabel)
        addSubview(bottomBorderView)

        addConstraintsWithFormat("H:|-8-[v0]-8-|", views: nameLabel)
        addConstraintsWithFormat("V:|[v0][v1(0.5)]|", views: nameLabel, bottomBorderView)

        addConstraintsWithFormat("H:|[v0]|", views: bottomBorderView)
    }

     }

     class FriendRequestCell: UITableViewCell {

       override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
         super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
    //    setupViews()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    }

}

所以,在我添加backgroundColor并删除图像时,它只是消失了(

1 个答案:

答案 0 :(得分:1)

据我所知,您应该FriendRequestCell方法获得cellForRowAt indexPath

请找到这个,

我为Cell创建 ListTableCell xib ,请在此处找到我的 ListTableCell

class ListTableCell: UITableViewCell {

    @IBOutlet weak var imgView: UIImageView!
    @IBOutlet weak var lblTemp: 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
    }
}

在ViewController上,

override func viewDidLoad() {
    super.viewDidLoad()

    let nib = UINib(nibName: "ListTableCell", bundle: nil)
    tblList.register(nib, forCellReuseIdentifier: "ListTableCell")

    getItems()
}

func getItems() {
    for i in 0...20 {
        arrItems.append("Test \(i)")
    }
}


extension Tab2VC: UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arrItems.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ListTableCell", for: indexPath) as! ListTableCell
        //cell.imgView.image = #imageLiteral(resourceName: "yelp")
        cell.imgView.backgroundColor = UIColor.red
        cell.lblTemp.text = arrItems[indexPath.row]
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
}

它适合我,请尝试这个。