【问题标题】:-[__NSCFString boundingRectWithSize:options:attributes:context:]: unrecognized selector sent to instance-[__NSCFString boundingRectWithSize:options:attributes:context:]: 无法识别的选择器发送到实例
【发布时间】:2014-01-16 06:53:28
【问题描述】:
@interface PromotionsListViewController : UITableViewController 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"PromotionCell";
PromotionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[PromotionCell alloc] init];
}

// Configure the cell...
Promotion *promotion = [self.promotionList objectAtIndex:indexPath.row];

[cell.imgView setImageWithURL:[NSURL URLWithString:promotion.imageURL] placeholderImage:[UIImage imageNamed:@""]];
cell.lblTitle.text = promotion.promotionTitle;

return cell;
}
@interface PromotionCell : UITableViewCell

@property(nonatomic, strong) UIImageView *imgView;
@property(nonatomic, strong) UILabel *lblTitle;

@end
- (void)layoutSubviews {

if (self.lblTitle.text) {

    CGSize maxsize = CGSizeMake(300, CGFLOAT_MAX);
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;

    CGRect calculateRect = [_lblTitle.text boundingRectWithSize:maxsize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:_lblTitle.font, NSParagraphStyleAttributeName:paragraphStyle.copy} context:nil];

    _lblTitle.frame = CGRectMake(_lblTitle.frame.origin.x, 300 - calculateRect.size.height - 5, _lblTitle.frame.size.width, calculateRect.size.height);

} else {
    _lblTitle.frame = CGRectZero;
}
}
- (UILabel *)lblTitle {

if (!_lblTitle) {

    _lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, 250, 300, 50)];
    _lblTitle.font = [UIFont boldSystemFontOfSize:22.0f];
    _lblTitle.numberOfLines = 0;
    _lblTitle.lineBreakMode = NSLineBreakByWordWrapping;
    [self.contentView addSubview:_lblTitle];
}
return _lblTitle;
}

谁能告诉我我做错了什么?我希望我的问题很清楚..

【问题讨论】:

  • 我认为错误消息很清楚...您正在对无法识别该方法的对象调用 boundingRectWithSize 。错误在哪里?您可以删除与错误消息无关的代码吗?
  • 您是在 iOS 7 模拟器/设备或以前的设备上运行此代码吗?在 iOS 7 中引入 boundingRectWithSize: 方法时,在以前的 iOS 版本上运行会抛出此错误。
  • @Geek 这不准确。 boundingRectWithSize: 在 iOS 6 中引入。来源:Apple Developer Library

标签: ios7 uitableview unrecognized-selector


【解决方案1】:

我也有这个问题。原来boundingRectWithSize: 有两种类似的方法。一种用于NSString,一种用于NSAttributedString

以下 NSAttributedString 可用于 iOS 6 及更高版本:

- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options context:(NSStringDrawingContext *)context

另一个 NSString 方法(您当前正在使用)仅在 iOS 7 上可用:

- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context

因此,只需在属性字符串上调用 boundingRectWithSize: 方法而不需要额外的 Attributes: 字段,它就可以在 iOS 6 上正常工作。

【讨论】:

  • @Jack Dewhurst,我删除了Attributes:,但错误:No visible @interface for NSString declares the selector boundingRectWithSize:options:context:
  • 通过将NSString转换为NSAttributedString解决
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多