2014-01-16 55 views
3
@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; 
} 

任何人都可以告訴我我做錯了什麼?我希望我的問題很明確..- [__ NSCFString boundingRectWithSize:options:attributes:context:]:無法識別的選擇器發送到實例

+0

我認爲這個錯誤信息非常清楚......您正在對無法識別該方法的對象調用boundingRectWithSize。錯誤位於哪裏?你能否刪除與錯誤信息無關的代碼? –

+0

您是否在iOS 7模擬器/設備或以前版本上運行此代碼?運行於以前的iOS版本將引發此錯誤,因爲在iOS 7中引入了「boundingRectWithSize:」方法。 – Geek

+0

@Geek這並不準確。 'boundingRectWithSize:'在iOS 6中引入。來源:[Apple Developer Library](https://developer.apple.com/library/ios/documentation/uikit/reference/NSAttributedString_UIKit_Additions/Reference/Reference.html#//apple_ref/) occ/instm/NSAttributedString/boundingRectWithSize:options:context :) –

回答

8

我也有這個問題。原來有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上正常工作。

+0

爲我節省了大量的學習時間。謝謝你。 – John

+0

@Jack Dewhurst,我刪除了'Attributes:',但是錯誤:'NSString沒有可見的@interface聲明選擇器boundingRectWithSize:options:context:' – Gank

+0

通過將NSString轉換爲NSAttributedString – Gank

相關問題