【问题标题】:Text of label in CustomUITableView is not changingCustomUITableView 中标签的文本没有改变
【发布时间】:2015-08-24 12:48:51
【问题描述】:

我使用下面的代码来更改我的自定义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
}

println() 日志的结果是:

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

但在设备(模拟器)中文本没有改变!

【问题讨论】:

  • 您在设备上看到了什么文字?

标签: ios swift uitableview uilabel


【解决方案1】:

如果您使用情节提要,请确保您已将标签连接到 insuranceName 插座(如果没有,只需从标签控制并拖动到您在助理检查器中找到的插座)。

另外,请使用println(cell.insuranceName.text),而不仅仅是println(cell.insuranceName)

【讨论】:

    【解决方案2】:

    从下面重构您的代码。 1.在viewDidLoad中注册nib 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";
                }
    
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-25
      相关资源
      最近更新 更多