Ios 分配自定义UITableViewCell的属性 我正在使用下面的自定义UITableViewCell,没有nib文件。 在UITableViewController中查看它没有问题。 我的问题是为名称标签cell分配文本。name.text=名称。。没有分配任何内容

Ios 分配自定义UITableViewCell的属性 我正在使用下面的自定义UITableViewCell,没有nib文件。 在UITableViewController中查看它没有问题。 我的问题是为名称标签cell分配文本。name.text=名称。。没有分配任何内容,ios,uitableview,swift,Ios,Uitableview,Swift,你能帮我吗 import UIKit class ACMenuCell: UITableViewCell { @IBOutlet var name : UILabel! override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) name = UI

你能帮我吗

import UIKit

class ACMenuCell: UITableViewCell {
    @IBOutlet var name : UILabel!
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        name = UILabel(frame: CGRectMake(20, 10, self.bounds.size.width , 25))
        name.backgroundColor = .whiteColor()
        self.contentView.addSubview(name)
        self.contentView.backgroundColor = .blueColor()
    }
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    override func layoutSubviews() {
        super.layoutSubviews()
    }
    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
    }

}

您可能希望尝试以下操作:在UITableViewCell的子类中创建一个公共函数,并在该函数中将文本设置为label

真空初始化池 { 执行类似于为标签设置文本的操作,标签是tableViewCell的子视图 } 从cellForRowAtIndexPAth,对cell对象调用此函数:

// taking your code for demo

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let reuseIdentifierAC:NSString = "ACMenuCell"; var cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifierAC, forIndexPath:indexPath) as ACMenuCell cell = ACMenuCell(style: UITableViewCellStyle.Default, reuseIdentifier: reuseIdentifierAC) [cell initializeCell] return cell }

这种方法对我很有效。

请您添加以下方法的代码。?func tableview tableview:UITableView!,cellForRowAtIndexPath索引路径:NSIndexPath!->UITableViewCell!self.bounds.size.width在init中很可能为0。如果您使用的是下面的自定义UITableViewCell而没有nib文件,则可能是的副本。您是如何为名称UILabel创建IBOutlet的。您使用的是故事板原型单元吗?@AbhishekSharma以下是方法:重写func tableViewtableView:UITableView,cellForRowAtIndexPath:nsindepath->UITableViewCell{让reuseIdentifierAC:NSString=ACMenuCell;var cell=tableView.dequeueReusableCellWithIdentifierreuseIdentifierAC,forIndexPath:indexPath作为ACMenuCell=ACMenuCellstyle:UtableViewCellStyle.Default,reuseIdentifier:reuseIdentifierAC cell.name.text=a name cell.name.textColor=.blackColor return cell}谢谢,我只是通过从cellforrowatinexpath方法中删除cell=ACMenuCellstyle:UITableViewCellStyle.Default,reuseIdentifier:reuseIdentifierAC来解决问题