简体   繁体   中英

Custom UITableViewCell created from .xib doesn't show

I'm lost. I searched and searched and cannot find the reason why my custom cell isn't displayed.

//  ProfileCell.swift:

import UIKit
import QuartzCore

class ProfileCell: UITableViewCell {

    @IBOutlet weak var profileNameLabel:    UILabel!
    @IBOutlet weak var profilePictureView:  UIImageView!
}

The second default TableViewCell is displayed normally. I have no missing constraints in Interface Builder or any Errors. ProfileCell is selected in the ProfileCell.xib identity tab as the custom class.

//  MoreTableViewControllerIB.swift

import UIKit

class MoreTableViewControllerIB: UITableViewController {

override func viewDidLoad() {
    super.viewDidLoad()
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {       
    return 2
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //  this cell is missing
        if indexPath.row == 0 {

            tableView.register(UINib(nibName: "ProfileCell", bundle: nil), forCellReuseIdentifier: "ProfileCell")
            let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileCell", for: indexPath) as! ProfileCell            
            cell.profileCommentLabel.text = "Test Comment Label"
            cell.profilePictureView.image = UIImage(named:"profile_picture_test")            
            return cell

        // this cell is displayed perfectly
        }else if indexPath.row == 1 {

            let cell = tableView.dequeueReusableCell(withIdentifier: "statisticsCell") ?? UITableViewCell(style: .default, reuseIdentifier: "statisticsCell")
            cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
            cell.textLabel?.text = "Statistics"
            cell.imageView.image = UIImage(named:"statistics")
            return cell

        // has to return a cell in every scenario
        }else{
            let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell
            return cell
        }
    }
}

Here is a screenshot of what I get.

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

在 viewDidLoad 或 viewWillApppear 中添加这一行

So I found out what my mistake was. Pretty stupid and it cost me half a day:

The cell was already displayed, but the default height wasn't big enough to see it. I thought the set height in the .xib would be used. It apparently is not. So I added this:

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if indexPath.row == 0 {
        return 192 // the height for custom cell 0
    }
}

在我的情况下,我必须另外注册 nib 以查看我的手机:

myTableView.register(UINib(nibName: "nibwithcell", bundle: nil), forCellReuseIdentifier: "cell") // you need to register xib file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM