2013-07-07 14 views
0

我有表格視圖和自定義單元格。我的自定義單元格有多行標籤,因此我必須執行代碼來更改標籤的高度。我把它放到layoutSubviews。 但我有麻煩。單元格顯示不正確。我開始用NSLog進行debagging。 我發現layoutSubviews調用不是最後(我檢查了init方法,layoutSubview,willDisplayCell(表視圖委託方法),drawRect和prepareForSegue)。最後一個是drawRect方法。我的解決方案是在drawRect中調用layoutSubviews,這有助於第一次顯示錶格視圖。但如果我切換視圖drawRect方法不再被調用,我有同樣的錯誤。如果你願意,我可以顯示截圖,但我真的需要你的建議,夥計們。在uitableviewcell中最後調用的方法是什麼?

PS:英語是我的主要麻煩,我知道,對不起。 :)

回答

0

我自己發現了真棒解決方案。訣竅是在表格視圖數據源NSArray中創建已經有圓角的圖像。我做了這樣的方向:

1.1添加QuartzCore框架項目。

1.2然後爲UIImage添加類別。的UIImage + RoundedCorners.h:

#import <UIKit/UIKit.h> 

    @interface UIImage (RoundedCorners) 

    - (UIImage *)imageWithRoundedCorners:(CGFloat)radius; 

    @end 

1.3的UIImage + RoundedCorners.m:

#import "UIImage+RoundedCorners.h" 
    #import <QuartzCore/QuartzCore.h> 

    @implementation UIImage (RoundedCorners) 

    - (UIImage *)imageWithRoundedCorners:(CGFloat)radius; 
    { 
     CALayer *imageLayer = [CALayer layer]; 
     imageLayer.frame = CGRectMake(0, 0, self.size.width, self.size.height); 
     imageLayer.contents = (id)self.CGImage; 

     imageLayer.masksToBounds = YES; 
     imageLayer.cornerRadius = radius; 

     UIGraphicsBeginImageContext(self.size); 
     [imageLayer renderInContext:UIGraphicsGetCurrentContext()]; 
     UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 

     return roundedImage; 
    } 

    @end 

2.1創建的NSArray(NSMutableArray的)與圖像,你要使用:

_thumbnailImages = [[NSMutableArray alloc] initWithCapacity:[_dishes count]]; 
    for (int counter = 0; counter < [_dishes count]; counter++) 
    { 
     EBEntityDish *dish = [_dishes objectAtIndex:counter]; 
     [_thumbnailImages addObject:[dish.image.thumbnailImage imageWithRoundedCorners:IMAGE_CORNER_RADIUS]]; 
    } 

2.2備註:

IMAGE_CORNER_RADIUS == your image corner radius. 
    _dishes - My objects that contain images small size (144 x 144) 

2.3結果:

My FPS increased on 10 - 20 FPS! Really awesome! Good luck! 
相關問題