【问题标题】:set dynamic text to uilabel in storyboard在情节提要中将动态文本设置为 uilabel
【发布时间】:2013-05-28 09:11:18
【问题描述】:

我正在使用 Storyboard,因为我有一个自定义表格。我需要在单元格内的标签上显示值。我尝试为它创建一个 IBOutlet,但它似乎不接受它,它给我一个错误提示,“连接“xyz”不能将原型对象作为其目标”

【问题讨论】:

    标签: storyboard uilabel iboutlet


    【解决方案1】:

    找到解决方案,我动态创建了一个标签并为其设置值 ` if (indexPath.row == 2) { 静态 NSString *Identifier1 = @"Cell3";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier1];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier1];
            cell.textLabel.lineBreakMode=NSLineBreakByCharWrapping;
            cell.textLabel.numberOfLines = 0;
            cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:15.0];
    
        }
    
        UILabel *txtDate = [[UILabel alloc] initWithFrame:CGRectMake(13.0f, 26.0f, 294.0f, 30.0f)];
        txtDate.text = stringFromDate;
    
        [txtDate setUserInteractionEnabled:NO];
        txtDate.font = [UIFont fontWithName:@"Helvetica" size:15.0];
        [cell.contentView addSubview:txtDate];
    
        return cell;
    
    
    }`
    

    【讨论】:

      【解决方案2】:

      在具有动态单元格的表格视图中,您不能将表格视图控制器中的 IBOutlet 连接到单元格内的元素,因为单元格是原型对象。如果您将表格视图设置为具有静态单元格,情况将有所不同。

      What's the difference between static cells and dynamic prototypes?

      由于您使用的是动态单元格,您需要将 UITableViewCell 子类化,为单元格子类中的标签创建一个插座,并将其连接到您的 Storyboard 中的 UILabel。

      如果您事先知道行在数量等方面不会改变,另一种选择就是使用静态表格视图单元格。

      【讨论】:

        【解决方案3】:

        iOS 10+ & Swift 3+,使用这个

        let Identifier1 = "Cell3"
        
        var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(Identifier1)
                if cell == nil {
                    cell = UITableViewCell(style: UITableViewCellStyleDefault, reuseIdentifier: Identifier1)
                    cell.textLabel.lineBreakMode = NSLineBreakByCharWrapping
                    cell.textLabel.numberOfLines = 0
                    cell.textLabel.font = UIFont(name: "Helvetica", size: 15.0)
                }
                var txtDate: UILabel = UILabel(frame: CGRectMake(13.0, 26.0, 294.0, 30.0))
                txtDate.text = stringFromDate
                txtDate.userInteractionEnabled = false
                txtDate.font = UIFont(name: "Helvetica", size: 15.0)
                cell.contentView.addSubview(txtDate)
                return cell
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-11-11
          • 2016-02-20
          • 1970-01-01
          • 1970-01-01
          • 2023-01-26
          • 2012-05-23
          • 2015-06-07
          相关资源
          最近更新 更多