2012-03-29 49 views
1

我有一個問題,也許是非常基本的。 我正在以編程方式構建一個沒有Xib文件的自定義單元。在我的CustomHell.m文件中,我基本上只有兩個方法:initWithStyle和layoutSubviews。 我已經設法以編程方式添加一些標籤和一個按鈕。現在我想添加一個特定的自定義視圖 - FacebookLikeView(https://github.com/brow/FacebookLikeView) - 但我不知道該怎麼做。 這是在我的「initWithStyle代碼:reuseIdentifier:如何在自定義單元格上以編程方式添加自定義視圖?

//StoreName Label 
    storeName = [[UILabel alloc] init]; 
    storeName.textAlignment = UITextAlignmentLeft; 
    storeName.font = [UIFont boldSystemFontOfSize: 16]; //12 
    storeName.backgroundColor = [UIColor clearColor]; 
    storeName.adjustsFontSizeToFitWidth = YES; 

    //checkIn Button 
    checkInButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    checkInButton.frame = CGRectMake(203,10,86,25); 
    [checkInButton setImage:[UIImage imageNamed:@"Check-In.png"] forState:UIControlStateNormal]; 
    [checkInButton addTarget:self.nextResponder action:@selector(addCheckin:) forControlEvents:UIControlEventTouchUpInside]; 
    checkInButton.backgroundColor = [UIColor redColor]; 

    //FACEBOOK 
    facebookLikeButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    facebookLikeButton.frame = CGRectMake(169,40,119,25); 

    [self.contentView addSubview:storeName]; 
    [self.contentView addSubview:storeAddress]; 
    [self.contentView addSubview:subtitle]; 
    [self.contentView addSubview:checkInButton]; 
    [self.contentView addSubview:facebookLikeButton]; 
    //[self.contentView addSubview:likeButton]; 
return self; 

這是我的layoutSubviews:

[super layoutSubviews]; 
CGRect contentRect = self.contentView.bounds; 
CGFloat boundsX = contentRect.origin.x; 

storeName.frame = CGRectMake(boundsX+10, 15, 190, 20); 
storeAddress.frame = CGRectMake(boundsX+10, 42, 200, 40); 
subtitle.frame = CGRectMake(boundsX+10, 77, 280, 30); 
likeButton.frame = CGRectMake(boundsX+199, 42, 50, 20); 

頭文件看起來是這樣的:

#import "FacebookLikeView.h" 
@interface CustomCellHead : UITableViewCell 
{ 
FacebookLikeView *likeButton; 

UIButton *facebookLikeButton; 
UIButton *checkInButton; 
UILabel *storeName; 
UILabel *storeAddress; 
UILabel *subtitle; 
} 

@property(nonatomic,retain) FacebookLikeView *likeButton; 
@property(nonatomic,retain) UIButton *checkInButton; 
@property(nonatomic,retain) UILabel *storeName; 
@property(nonatomic,retain) UILabel *storeAddress; 
@property(nonatomic,retain) UILabel *subtitle; 

@end 

回答

0

嘗試[self addSubview: storeName]代替[self.contentView addSubview:storeName]

+0

這似乎沒有w掃。但是,我通過使用XIB文件並通過IB連接FacebookLikeView來解決此問題。 – Giovanni 2012-04-11 23:06:23

相關問題