【问题标题】:Unable to Access UILabel in UITableViewCell无法在 UITableViewCell 中访问 UILabel
【发布时间】:2009-10-03 11:34:54
【问题描述】:

我在访问 UITableViewCell 中的内容时遇到问题,我已将其放置在我的主 .xib 文件中。

该标签连接到 IBOutlet servicesCell。

并且table view cell里面的Label是通过IBOutlet serviceLabel连接的。

在运行时,我没有得到我要添加到该标签的文本。

以下是我的示例代码。

static NSString *ServiceIdentifier = @"ServiceIdentifier";

UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:ServiceIdentifier];

if(cell1 == nil) {
    [[NSBundle mainBundle] loadNibNamed:@"servicesCell" owner:self options:nil];
    cell1 = servicesCell;
}

// label access
serviceLabel = (UILabel *)[cell1 viewWithTag:1];
serviceLabel.numberOfLines = 3;
serviceLabel.lineBreakMode = UILineBreakModeWordWrap;
[serviceLabel setTextAlignment:UITextAlignmentLeft];
[serviceLabel setText:@"Testing String"];

任何人有任何想法,请帮助..

提前谢谢..

【问题讨论】:

    标签: iphone uitableview uilabel


    【解决方案1】:

    您说您的 Cell 位于您的主 XIB 文件中,但我认为该单元实际上位于“servicesCell.xib”中,对吗?

    我假设您从 "Loading Custom Table-View Cells from Nib Files" 开始。此代码类似于 Apple 的示例,因此我假设您从那里开始。如果您只是从某个地方复制了这段代码而没有理解它,请先阅读该文档。

    您应该在调试器中遍历此代码,并确保所有内容都设置为您认为应该设置的值。 “什么都没有发生”的最常见原因是您正在向 nil 发送消息,所以我敢打赌这里的变量之一实际上是 nil。查看这段代码,最可能的罪魁祸首是未能在 IB 中实际连接某些东西(这是使用 NIB 时出现 nil 对象的最常见原因;仔细检查您是否真的做到了),或者您没有为您的标签分配预期的标签 1。

    【讨论】:

      【解决方案2】:

      将 UITableViewCell 放在它自己的 nib 中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-12
        相关资源
        最近更新 更多