iOS 重写UITableViewCell之动态获取label文字的宽度进行布局
2016-01-12 17:57 甘雨路 阅读(620) 评论(0) 编辑 收藏 举报
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
#import "AppDelegate.h" #import "MovieHomeController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:[[MovieHomeController alloc] init]]; self.window.rootViewController = navi; [self.window makeKeyAndVisible]; return YES; } @end
#import <UIKit/UIKit.h> @interface MovieHomeController : UIViewController @end
#import "MovieHomeController.h" #import "CinemaInfomationCell.h" //#import "SGFocusImageFrame.h" #define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue] #define IS_IOS7 (IOS_VERSION >= 7.0 ? YES : NO) #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width #define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height #define scale SCREEN_WIDTH/320.0 #define advertScrollView_height 100*scale @interface MovieHomeController ()<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate> { // UIScrollView *_advertScrollView; // NSMutableArray *advertImages; UITableView *_tableView; NSMutableArray *datas; } //@property(nonatomic, strong) SGFocusImageFrame *bannerView;//banner栏 @end @implementation MovieHomeController - (void)viewDidLoad { [super viewDidLoad]; self.title = @"电影"; datas = [[NSMutableArray alloc] init]; NSArray *arr = @[@"哈艺时尚影城(白云YH城店)",@"20%",@"5",@"广州市白云区广州大道北28号梅花园商业中心302",@"6.2km",@"23",@"座",@"主题",@"满"];//立开 [datas addObject:arr]; self.view.backgroundColor = [UIColor lightGrayColor]; _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain]; _tableView.dataSource = self; _tableView.delegate = self; [self.view addSubview:_tableView]; } #pragma mark -- tableView 的数据源配置 -- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return datas.count; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 100; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *identifier = @"cell"; CinemaInfomationCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil) { cell = [[CinemaInfomationCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; } NSArray *info = datas[indexPath.row]; cell.cinemaName.text = info[0]; cell.rebateAccount.text = [NSString stringWithFormat:@"返%@",info[1]]; cell.score.text = [NSString stringWithFormat:@"%@分",info[2]]; cell.address.text = info[3]; cell.distance.text = info[4]; cell.price.text = [NSString stringWithFormat:@"¥ %@",info[5]]; cell.style.text = info[8]; cell.theme.text = info[7]; cell.seat.text = info[6]; // NSLog(@"******%@",info[0]); return cell; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
#import <UIKit/UIKit.h> @interface CinemaInfomationCell : UITableViewCell @property (nonatomic, strong) UILabel *cinemaName;//电影院名称 @property (nonatomic, strong) UILabel *rebateAccount;//返利金 @property (nonatomic, strong) UILabel *score; //等级评分 @property (nonatomic, strong) UILabel *address;//地址 @property (nonatomic, strong) UILabel *price;//价格 @property (nonatomic, strong) UILabel *distance;//距离 @property (nonatomic, strong) UILabel *seat;//座 @property (nonatomic, strong) UILabel *theme;//主题 @property (nonatomic, strong) UILabel *style;//满或其它的 @end
#import "CinemaInfomationCell.h" #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width #define top_height 5 #define cinemaName_height 30 #define rebateAccount_height 17 #define address_height 30 #define price_height 30 @interface CinemaInfomationCell () { UIView *line;//底部的细线 UILabel *label;//起字 UIView *verticalLine1;//竖线 UIView *verticalLine2;//竖线 } @end @implementation CinemaInfomationCell - (void)awakeFromNib { // Initialization code } - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { self.cinemaName = [[UILabel alloc] init]; [self addSubview:self.cinemaName]; self.rebateAccount = [[UILabel alloc] init]; self.rebateAccount.backgroundColor = [UIColor colorWithRed:60/255.0 green:229/255.0 blue:170/255.0 alpha:1]; self.rebateAccount.font = [UIFont systemFontOfSize:10]; self.rebateAccount.textColor = [UIColor whiteColor]; self.rebateAccount.textAlignment = NSTextAlignmentCenter; self.rebateAccount.layer.cornerRadius = 5; self.rebateAccount.clipsToBounds = YES; [self addSubview:self.rebateAccount]; self.score = [[UILabel alloc] init]; self.score.layer.borderColor = [[UIColor colorWithRed:240/255.0 green:162/255.0 blue:140/255.0 alpha:1] CGColor]; self.score.layer.borderWidth = 1.5; self.score.font = [UIFont systemFontOfSize:10]; self.score.textColor = [UIColor colorWithRed:240/255.0 green:162/255.0 blue:140/255.0 alpha:1]; self.score.layer.cornerRadius = 5; self.score.clipsToBounds = YES; self.score.textAlignment = NSTextAlignmentCenter; [self addSubview:self.score]; self.address = [[UILabel alloc] init]; self.address.textColor = [UIColor grayColor]; self.address.font = [UIFont systemFontOfSize:11]; self.address.backgroundColor = [UIColor clearColor]; [self addSubview:self.address]; self.distance = [[UILabel alloc] init]; self.distance.textColor = [UIColor grayColor]; self.distance.font = [UIFont systemFontOfSize:11]; self.distance.backgroundColor = [UIColor clearColor]; self.distance.textAlignment = NSTextAlignmentCenter; [self addSubview:self.distance]; self.price = [[UILabel alloc] init]; self.price.backgroundColor = [UIColor clearColor]; self.price.font = [UIFont systemFontOfSize:17]; self.price.textColor = [UIColor colorWithRed:182/255.0 green:0 blue:76/255.0 alpha:1.0]; self.price.textAlignment = NSTextAlignmentRight; [self addSubview:self.price]; //起字 label = [[UILabel alloc] init]; label.backgroundColor = [UIColor clearColor]; label.textColor = [UIColor colorWithRed:182/255.0 green:0 blue:76/255.0 alpha:1.0]; label.text = @" 起"; label.textAlignment = NSTextAlignmentLeft; label.font = [UIFont systemFontOfSize:12]; [self addSubview:label]; self.style = [[UILabel alloc] init]; self.style.backgroundColor = [UIColor clearColor]; self.style.font = [UIFont systemFontOfSize:12]; self.style.textColor = [UIColor grayColor]; [self addSubview:self.style]; verticalLine1 = [[UIView alloc] init]; verticalLine1.backgroundColor = [UIColor grayColor]; [self addSubview:verticalLine1]; self.theme = [[UILabel alloc] init]; self.theme.backgroundColor = [UIColor clearColor]; self.theme.font = [UIFont systemFontOfSize:12]; self.theme.textColor = [UIColor grayColor]; self.theme.textAlignment = NSTextAlignmentCenter; [self addSubview:self.theme]; verticalLine2 = [[UIView alloc] init]; verticalLine2.backgroundColor = [UIColor grayColor]; [self addSubview:verticalLine2]; self.seat = [[UILabel alloc] init]; self.seat.backgroundColor = [UIColor clearColor]; self.seat.font = [UIFont systemFontOfSize:12]; self.seat.textColor = [UIColor grayColor]; self.seat.textAlignment = NSTextAlignmentRight; [self addSubview:self.seat]; //底部的细线 line = [[UIView alloc] init]; line.backgroundColor = [UIColor colorWithRed:217/255.0 green:217/255.0 blue:217/255.0 alpha:1]; [self addSubview:line]; } return self; } - (void)layoutSubviews{ [super layoutSubviews]; //计算文字的宽度 self.cinemaName.numberOfLines = 1; UIFont *font = [UIFont fontWithName:@"Arial" size:15]; self.cinemaName.font = font; CGSize constraint = CGSizeMake(self.frame.size.width - 80, 20000); CGSize size = [self.cinemaName.text sizeWithFont:font constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping]; NSLog(@"===%@==%f",self.cinemaName.text,size.width); self.cinemaName.frame = CGRectMake(5, top_height, size.width+5,cinemaName_height); self.rebateAccount.frame = CGRectMake(CGRectGetMaxX(self.cinemaName.frame), CGRectGetMidY(self.cinemaName.frame) - rebateAccount_height/2.0, 40, rebateAccount_height); self.score.frame = CGRectMake(CGRectGetMaxX(self.rebateAccount.frame)+3, self.rebateAccount.frame.origin.y, 25, rebateAccount_height); self.address.frame = CGRectMake(5, CGRectGetMaxY(self.cinemaName.frame), SCREEN_WIDTH - 70, address_height); self.distance.frame = CGRectMake(SCREEN_WIDTH - 60, self.address.frame.origin.y, 60, address_height); self.price.frame = CGRectMake(0, CGRectGetMaxY(self.address.frame), 55, price_height); label.frame = CGRectMake(CGRectGetMaxX(self.price.frame), self.price.frame.origin.y+2, 20, price_height); self.style.frame = CGRectMake(SCREEN_WIDTH - 30, CGRectGetMaxY(self.address.frame), 25, price_height); verticalLine1.frame = CGRectMake(CGRectGetMinX( self.style.frame) - 3, CGRectGetMidY(self.style.frame)-10/2.0, 1, 10); self.theme.frame = CGRectMake(CGRectGetMinX(verticalLine1.frame)-24-2, CGRectGetMinY(self.style.frame), 24, price_height); verticalLine2.frame = CGRectMake(CGRectGetMinX(self.theme.frame)-3, CGRectGetMidY(self.style.frame)-10/2.0, 1, 10); self.seat.frame = CGRectMake(CGRectGetMinX(verticalLine2.frame)-25-2, CGRectGetMinY(self.style.frame), 25, price_height); line.frame = CGRectMake(0,self.frame.size.height - 1,SCREEN_WIDTH,1); } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { // [super setSelected:selected animated:animated]; // Configure the view for the selected state } - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{ } @end