简体   繁体   English

CustomUITableView中的标签文本未更改

[英]Text of label in CustomUITableView is not changing

I use below code to change text of the lable of my custom uitableviewcell : 我使用下面的代码来更改自定义uitableviewcell的标签文本:

   override func  tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    //NSArray *allsubviews = [[NSBundle mainBundle] loadNibNamed:@"LICustomUI" owner:nil options:nil];
    //LIMerchantTableViewCell *cell = [allsubviews objectAtIndex:LIApplicationLayout() == LIApplicationDirectionRightToLeft ? 8 : 9];

    let allSubViews = NSBundle.mainBundle().loadNibNamed("LICustomUI", owner: nil, options: nil)

    var cell:LIInsuranceNameTableViewCell = allSubViews[11] as! LIInsuranceNameTableViewCell

    cell.insuranceName.text = "Hello"

    println(cell.insuranceName)

    cell.insuranceName.backgroundColor = UIColor.redColor();

   // cell.backgroundColor = UIColor.redColor();

    return cell
}

the result of the println() log is: println()日志的结果是:

<UILabel: 0x7ad71930; frame = (0 11; 320 21); text = 'Hello'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7ad71a10>>

but in the device (emulator) the text is not changed! 但是在设备(仿真器)中,文本未更改!

If you're using storyboards, make sure you have connected the label to the insuranceName outlet (if you haven't, just control-drag from the label to the outlet you find within the Assistant inspector). 如果使用情节提要板,请确保已将标签连接到insuranceName插座(如果尚未将其连接,只需按住Control键将其从标签拖到助手检查器中找到的插座)。

Also, use println(cell.insuranceName.text) , and not just println(cell.insuranceName) . 另外,使用println(cell.insuranceName.text) ,而不仅仅是println(cell.insuranceName)

Restructure your code from below. 从下面重组代码。 1. Register the nib in viewDidLoad 2. deque in cellForRowAtIndexPath 1.在viewDidLoad中注册笔尖2.在cellForRowAtIndexPath中将双端队列注册

override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.registerNib( UINib(nibName:"nibName", bundle: nil), forCellReuseIdentifier: "identifier");
    }

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let myTableCell = tableView.dequeueReusableCellWithIdentifier("identifier", forIndexPath: indexPath) as! UITableViewCell;
        let subViews     = myTableCell.contentView.subviews as! Array
        if subViews.count > 0 {
            if let myLabel = subViews[0] as! UILabel {
                myLabel.text = "All good to go";
            }

        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM