iOS Swift - UITableViewCell的自定义子类无法显示内容

7

我有一个UITableView,是在UIStoryboard中创建的,其中包含两个动态原型UITableViewCells:

UITableViewCells 在 Interface Builder 中的截图

如截图所示,第一个UITableViewCell的样式设置为Subtitle,第二个设置为自定义,并在中心添加了一个标签“Tap to Add”。第一个的标识符为“Cell”,第二个为“AddCell”。我已经设置了一个UITableViewController(我也尝试过UIViewController中的UITableView),用Swift编写了UITableViewCell子类,并连接了所有的outlets。但是,当我运行模拟器时,单元格被加载并且可点击,但我无法让它显示任何内容。(我尝试添加其他控件,但当单元格被加载时,没有任何东西会出现。唯一可以更改的是contentView的backgroundColor。)

下面是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
    }}

我还有下面的Swift代码用于UITableViewCell:
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

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

2个回答

10

我认为这与Storyboard中的大小设置有关。现在它似乎更喜欢默认为更宽的视图,而大多数人,包括我自己(并且根据你的故事板视图宽度判断,也是你),都喜欢将宽度设置为“紧凑”。

在Storyboard中,尝试将宽度/高度设置为任何/任何,或者在单元格内部的标签的检查器中向下滚动,并播放“Installed”复选框,您会注意到它有各种大小类的选项。如果像我的一样,您将取消选中第一个“已安装”选项,并选中第二个选项以进行大小调整。然后,我删除了自定义的“Installed”并勾选了默认选项,然后将我的标签移动到位。

我认为我的声望不足以发布超过1张图片或2个链接,但希望我能够更轻松地解释我的意思。


1
问题是我添加到UITableViewCell子类的标签在所选故事板大小上没有选择安装复选框。谢谢。 - Aron C
1
我的一些AutoLayout约束条件没有安装,所以也要检查它们! - Stuart Douglas

4

我花了很多时间研究这个问题,想知道为什么我的代码跟这里提到的问题一样却不能正常工作。我在使用storyboard和swift 2 (xcode 7.2)。

当我移除...

self.tableView.registerClass(AddListObjectTableViewCell.classForCoder(), forCellReuseIdentifier: "AddCell")"

在viewDidLoad()方法中,这对我很有用。我只是像示例中所述使用了dequeueReusableCellWithIdentifier()方法,填充了单元格,就完成了...

祝好, 米歇尔


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接