2013-05-21 103 views
0

我有一個UIView包含投影陰影和角落,我正在加載我的UIViewController中的四個,並且我看到屏幕加載時性能下降。由於我使用的陰影和角半徑相同的白色背景,我想我會將UIView存儲在NSCache中。UIView在NSCache被覆蓋

當我運行的應用程序有一個很大的差距,第一個UIView應該是,但它沒有顯示出來。顯示的是我的視圖列表中的最後一個視圖。如果我將最後一個註釋掉並再次運行,第三個顯示出來。這似乎是我在內存中的指針有問題,但不知道。也許我錯誤地使用了NSCache?

(注:不使用NSCache顯示的第一個視圖)

enter image description here

這裏是我如何使用NSCache:

.h文件中

@interface LunchDetailViewController : UIViewController <UIScrollViewDelegate> 

@property (nonatomic) IBOutlet UIScrollView *scrollView; 
@property (nonatomic, strong) NSCache *entreeViewsCache; 

@end 

.m文件

@synthesize scrollView; 
@synthesize entreeViewsCache; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.entreeViewsCache = [[NSCache alloc] init]; 
    UIView *entreeView = [[UIView alloc] init]; 
    entreeView.backgroundColor = [UIColor whiteColor]; 
    entreeView.layer.masksToBounds = NO; 
    entreeView.layer.cornerRadius = 3.0; 
    entreeView.layer.shadowOffset = CGSizeMake(1.1, 2.1); 
    entreeView.layer.shadowOpacity = 0.2; 

    [self.entreeViewsCache setObject:entreeView forKey:@"EntreeView"]; 
} 

- (void) configureScrollView 
{ 
    // This line of code allows the scroll view to be 'scrollable'. 
    self.scrollView.contentSize = CGSizeMake(320, 620); 

    UIView *elementaryRoundedCornerView = [self.entreeViewsCache objectForKey:@"EntreeView"]; 
    elementaryRoundedCornerView.frame = CGRectMake(15,15,290,180); 

    UIView *middleRoundedCornerView = [self.entreeViewsCache objectForKey:@"EntreeView"]; 
    middleRoundedCornerView.frame = CGRectMake(15,210,290,180); 

    UIView *highRoundedCornerView = [self.entreeViewsCache objectForKey:@"EntreeView"]; 
    highRoundedCornerView.frame = CGRectMake(15,404,290,180); 

    NSMutableArray *entreeItems = [[NSMutableArray alloc] initWithObjects:@"Pancakes w/ Sausage Patties", @"Corn Dog", @"Grilled Cheese Sandwhich", @"Chicken Tender Wraps", nil]; 


    UIView *elementaryLunchMenuDetails = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 240, 160)]; 
    [elementaryLunchMenuDetails addSubview:[self returnNativeCode:entreeItems rectDimensions:CGRectMake(2, 5, 215, 160) schoolType:@"Elementary"]]; 
    [elementaryRoundedCornerView addSubview:elementaryLunchMenuDetails]; 

    UIView *middleLunchMenuDetails = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 240, 160)]; 
    [middleLunchMenuDetails addSubview:[self returnNativeCode:entreeItems rectDimensions:CGRectMake(2, 2, 215, 160) schoolType:@"Middle"]]; 
    [middleRoundedCornerView addSubview:middleLunchMenuDetails]; 

    UIView *highLunchMenuDetails = [[UIView alloc] initWithFrame:CGRectMake(10,10, 240, 160)]; 
    [highLunchMenuDetails addSubview:[self returnNativeCode:entreeItems rectDimensions:CGRectMake(2, 2, 215, 160) schoolType:@"High"]]; 
    [highRoundedCornerView addSubview:highLunchMenuDetails]; 

    [self.scrollView addSubview:elementaryRoundedCornerView]; 
    [self.scrollView addSubview:middleRoundedCornerView]; 
    [self.scrollView addSubview:highRoundedCornerView]; 
} 

回答

0

哇。這很聰明。但不正確。

而不是使用NSCache複製視圖,您可能需要創建一個UIView子類,以您想要的方式設置視圖的格式。然後在你的滾動視圖上扔一堆視圖。

ABCView.m

@implementation ABCDayView 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     self.backgroundColor = [UIColor whiteColor]; 
     self.layer.masksToBounds = NO; 
     self.layer.cornerRadius = 3.0; 
     self.layer.shadowOffset = CGSizeMake(1.1f, 2.1f); 
     self.layer.shadowOpacity = 0.2f; 
    } 
    return self; 
} 

- (void)setItems:(NSArray *)items 
{ 
    if ([_items isEqualToArray:items] == NO) { 
     _items = items; 
     [self createItemViews]; 
     [self setNeedsLayout]; 
    } 
} 

// You'll also need to add -createItemViews and -setNeedsLayout methods. 

.m文件

- (void)configureScrollView 
{ 
    NSMutableArray *entreeItems = @[@"Pancakes w/Sausage Patties", 
            @"Corn Dog", 
            @"Grilled Cheese Sandwhich", 
            @"Chicken Tender Wraps"]; 

    CGRect frame = CGRectMake(15,15,290,180); 
    ABCDayView *elementaryView = [[ABCDayView alloc] initWithFrame:frame]; 
    elementaryView.items = entreeItems; 

    CGFloat y = CGRectGetMaxY(elementaryView.frame) + 10.0f; 
    frame = CGRectMake(15, y, 290, 180); 
    ABCDayView *middleView = [[ABCDayView alloc] initWithFrame:frame]; 
    middleView.items = entreeItems; 

    ... 

    CGFloat length = // Use CGRectGetMaxY on the last frame to get the length. 
    self.scrollView.contentSize = CGSizeMake(320, length); 
} 

這絕不是完美的代碼。但希望它會給你一個更好的實現方法的想法。