HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iOS Swift – UITableViewCell自定义子类不显示内容大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个UITableView,我在UIStoryboard中创建了两个动态原型UITableViewCells:

屏幕截图将向您显示我将第一个UITableViewCell的样式设置为Subtitle,第二个设置为自定义,并在中心添加标签“Tap to Add”.第一个具有标识符“Cell”和第二个“AddCell”.我已经设置了一个UITableViewController(我也在UIViewController中尝试了UITableView),Swift中的UITableViewCell子类,我连接了所有的插座.但是,当我运行模拟器时,单元格已加载并且它是可点击的,但我无法让它显示任何内容. (我已尝试添加其他控件,但在加载单元格时不会出现任何内容.我唯一能改变的是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
    }}

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

最后,这是模拟器的屏幕截图,选中时可见空单元格:

我已经仔细检查了所有我的所有插座都已连接,我的类名在Interface Builder中正确设置,我已经使用tableView注册了我的UITableViewCell类,所有内容似乎都配置正确.这可能是一个错误吗?任何帮助将不胜感激.

解决方法

我相信它与Storyboard中的大小有关.现在看来它似乎认为更广泛的视图,大多数人包括我(并根据你的故事板视图宽度,你)更喜欢将宽度设置为“紧凑”.

在故事板中,尝试将宽度/高度设置为任何/任何或在检查器中,单元格内的标签一直向下滚动并播放“已安装”复选框,您会发现它有大小类的各种选项.如果它像我的一样,你将第一个’已安装’一个未经检查,另一个你的尺寸选项选中.我删除自定义’已安装’并检查了认设置,然后将标签移动到位.

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

大佬总结

以上是大佬教程为你收集整理的iOS Swift – UITableViewCell自定义子类不显示内容全部内容,希望文章能够帮你解决iOS Swift – UITableViewCell自定义子类不显示内容所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。