swift - iOS Swift - UITableViewCell 自定义子类不显示内容

标签 swift

我有一个在 UIStoryboard 中创建的 UITableView,它有两个动态原型(prototype) UITableViewCells:

Screenshot of UITableViewCells in Interface Builder

屏幕截图将显示我将第一个 UITableViewCell 的样式设置为副标题,第二个设置为自定义,中间有一个标签“Tap to Add”。第一个标识符为“Cell”,第二个标识符为“AddCell”。我已经设置了一个 UITableViewController(我也在 UIViewController 中尝试了一个 UITableView),Swift 中的 UITableViewCell 子类,并且我已经连接了我所有的 socket 。但是,当我运行模拟器时,单元格已加载并且可以点击,但我无法让它显示任何内容。 (我试过添加其他控件,但加载单元格时什么也不会出现。我唯一可以更改的是 contentView 的背景颜色。)

我有以下用于 UITableViewController 的 Swift 代码:

import UIKit

class ListTableViewController: UITableViewController {

    var listObjects: ListObject[] = DataManager.instance.allListObjects() as ListObject[]

    init(style: UITableViewStyle) {
        super.init(style: style)
        // Custom initialization
    }

    init(coder aDecoder: NSCoder!) {
        super.init(coder: aDecoder)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.registerClass(AddListObjectTableViewCell.classForCoder(), forCellReuseIdentifier: "AddCell")
    }
    @IBAction func editButtonPressed(editButton: UIBarButtonItem) {
        if (self.tableView.editing) {
            editButton.title = "Edit"
            self.tableView.setEditing(false, animated: true)
        } else {
            editButton.title = "Done"
            self.tableView.setEditing(true, animated: true)
        }
    }

    // #pragma mark - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
        return listObjects.count + 1
    }


    override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {

        let cellIdentifier = (indexPath.row < listObjects.count) ? "Cell" : "AddCell"
        var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as UITableViewCell

        if (indexPath.row < listObjects.count) {
            let currentListObject : ListObject = listObjects[indexPath.row]
            cell.textLabel.text = currentListObject.name
            cell.detailTextLabel.text = currentListObject.detail
        } else {
            cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as AddListObjectTableViewCell
            if (cell == nil) {
                cell = AddListObjectTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellIdentifier)
            }
        }

        return cell
    }

    override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
        if (indexPath.row < listObjects.count) {

        } else {
            self.performSegueWithIdentifier("AddListObjectShow", sender: self)
        }

        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }

    // Override to support conditional editing of the table view.
    override func tableView(tableView: UITableView?, canEditRowAtIndexPath indexPath: NSIndexPath?) -> Bool {

        return (indexPath?.row < listObjects.count) ? true : false
    }}

我的 UITableViewCell 也有以下 Swift:

import UIKit

class AddListObjectTableViewCell: UITableViewCell {

    @IBOutlet var addLabel : UILabel

    init(style: UITableViewCellStyle, reuseIdentifier: String) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        // Initialization code
    }

    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
    }}

最后,这是模拟器的屏幕截图,选中时可以看到空单元格: enter image description here

我仔细检查了所有 socket 是否已连接,我的类名是否已在 Interface Builder 中正确设置,我已将 UITableViewCell 的类注册到 tableView,一切似乎都已正确配置。这可能是一个错误吗?任何帮助将不胜感激。

最佳答案

我认为这与 Storyboard 中的大小有关。现在它似乎喜欢默认为更宽的 View ,包括我在内的大多数人(根据你的 Storyboard View 宽度来判断,你)更喜欢将宽度设置为“紧凑”。

在 Storyboard 中,尝试将宽度/高度设置为任意/任意,或者在检查器中将单元格内的标签一直向下滚动并选中“已安装”复选框,您会发现它有多种尺寸选择类(class)。如果它像我的一样,您将取消选中第一个“已安装”,然后选中第二个用于调整大小的选项。我删除了自定义的“已安装”并选中了默认的,然后将我的标签移动到位。

我不相信我有足够的声誉来发布超过 1 张图片或 2 个链接,希望我能更容易地解释我的意思。

关于swift - iOS Swift - UITableViewCell 自定义子类不显示内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24421702/

相关文章:

swift - Swift 4 中的 AlamofireImage 问题

ios - Swift - 如何知道循环何时结束?

ios - 如何在 tableview 中显示姓名,就像在 iphone 联系人列表中一样,包含 A 到 Z 部分

ios - 分别关闭 ViewCntroller 的两个实例 Swift 3.0

ios - 文本在框架中未正确缩放

ios - 从另一个文件访问方法时,scrollView setContentOffset 崩溃

swift - 如何从 SKNode.children 数组访问 SKNode 子类方法?

ios - 目标 Action 模式的快速无法识别的选择器

swift - 模式 : Using a default value when optional is nil

ios - 如何使 SCNNode 面向 ARAnchor