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

iOS Swift – UITableViewCell自定义子类不显示内容,第1张

概述我有一个UITableView,我在UIStoryboard中创建了两个动态原型UITableViewCells: 屏幕截图将向您显示我将第一个UITableViewCell的样式设置为Subtitle,第二个设置为自定义,并在中心添加标签“Tap to Add”.第一个具有标识符“Cell”和第二个“AddCell”.我已经设置了一个UITableViewController(我也在UIView 我有一个UItableVIEw,我在UIStoryboard中创建了两个动态原型UItableVIEwCells:

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

我有UItableVIEwController的以下Swift代码:

import UIKitclass 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 UIKitclass 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自定义子类不显示内容所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1102213.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存