将文本设置为自定义表格单元格标签时发生崩溃
当我想设置属于表格单元格的标签文本时,我的应用程序崩溃了。我的代码片段如下:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您设置为 splitMethodLabel 的 tag 值似乎可能导致该问题。
只需将标记值更改为其他值并检查它是否仍然崩溃。
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.
啊,问题是:您将自己的标签作为子视图添加到单元格中,但没有给出引用 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.