将文本设置为自定义表格单元格标签时发生崩溃

发布于 2024-11-02 15:30:14 字数 1323 浏览 3 评论 0原文

当我想设置属于表格单元格的标签文本时,我的应用程序崩溃了。我的代码片段如下:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) 
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:simpleTableIdentifier];
    [cell autorelease];

    CGRect splitMethodLabelRect = CGRectMake(160, 6, 50, 30); 
    UILabel *splitMethodLabel = [[UILabel alloc] initWithFrame:splitMethodLabelRect]; 
    splitMethodLabel.textAlignment = UITextAlignmentLeft; 
    splitMethodLabel.font = [UIFont systemFontOfSize:13]; 
    splitMethodLabel.tag = kSplitMethodTag;
    [cell.contentView addSubview: splitMethodLabel]; 
    [splitMethodLabel release];

}

UILabel *splitMethodName = (UILabel *)[cell.contentView viewWithTag:kSplitMethodTag]; 

//app crashes at this point
splitMethodName.text = @"Test"; 

问题似乎出在我设置文本时。 下面的堆栈跟踪:

2011-04-21 15:11:10.820 BillSplitter[3021:707] -[UITableViewCellContentView setText:]: unrecognized selector sent to instance 0x18a680
2011-04-21 15:11:10.829 BillSplitter[3021:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCellContentView setText:]: unrecognized selector sent to instance 0x18a680'

非常感谢对此的任何建议!

My app crashes when I want to set the text of the label belonging to a table cell. My code snippet is as follows:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) 
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:simpleTableIdentifier];
    [cell autorelease];

    CGRect splitMethodLabelRect = CGRectMake(160, 6, 50, 30); 
    UILabel *splitMethodLabel = [[UILabel alloc] initWithFrame:splitMethodLabelRect]; 
    splitMethodLabel.textAlignment = UITextAlignmentLeft; 
    splitMethodLabel.font = [UIFont systemFontOfSize:13]; 
    splitMethodLabel.tag = kSplitMethodTag;
    [cell.contentView addSubview: splitMethodLabel]; 
    [splitMethodLabel release];

}

UILabel *splitMethodName = (UILabel *)[cell.contentView viewWithTag:kSplitMethodTag]; 

//app crashes at this point
splitMethodName.text = @"Test"; 

The issue seems to be at the point when I am setting the text.
Stacktrace below:

2011-04-21 15:11:10.820 BillSplitter[3021:707] -[UITableViewCellContentView setText:]: unrecognized selector sent to instance 0x18a680
2011-04-21 15:11:10.829 BillSplitter[3021:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCellContentView setText:]: unrecognized selector sent to instance 0x18a680'

Any advise on this is greatly appreciated!

Zhen

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

方圜几里 2024-11-09 15:30:14

您设置为 splitMethodLabeltag 值似乎可能导致该问题。
只需将标记值更改为其他值并检查它是否仍然崩溃。

It seems that the tag value you've set to splitMethodLabel could be causing the problem.
Just change the tag value to something else and check if it crashes still.

终陌 2024-11-09 15:30:14

啊,问题是:您将自己的标签作为子视图添加到单元格中,但没有给出引用 splitMethodName 的属性。该标签位于单元格的视图层次结构中,但您没有访问它的引用。

您可以通过子类化 UITableViewCell 并将标签添加为属性来解决此问题。然后使用您的自定义类。覆盖 initWithStyle,将参数传递给 super,然后创建标签,添加为子视图并分配给您的属性。

Ah the problem is: you are adding your own label to the cell as subview, but there is not a property given which references splitMethodName. The label is in the view hierarchy of the cell but you don't have a reference to access it.

You could fix this by subclassing UITableViewCell and add your label as a property. Use your custom class then. Override initWithStyle, pass the parameters to super, then create you label, add as subview AND assign to your property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文