iOS 绘制 cell --- 新手学习笔记。
#import @interface Animal : NSObject @property (strong, nonatomic) NSString *name; @property (strong, nonatomic) NSString *detail; @property (strong, nonatomic) NSString *imageName; @end
#import "Animal.h" @implementation Animal @end
#import @interface HRCell : UITableViewCell { UIView *contentView; } - (void)drawContentView:(CGRect)rect; @end
#import "HRCell.h" @class Animal; @interface HRCustomCell : HRCell @property (weak, nonatomic) NSString *nameText; @property (weak, nonatomic) NSString *detailText; @property (weak, nonatomic) NSString *imageName; - (void)bindAnimal:(Animal *)animal; @end
#import "HRCustomCell.h" #import "Animal.h" #define rNameFontSize 18.0f #define rDetailFontSize 14.0f static UIFont *NameFont; static UIFont *DetailFont; @implementation HRCustomCell + (void)initialize { NameFont = [UIFont fontWithName:@"American Typewriter" size:rNameFontSize]; DetailFont = [UIFont fontWithName:@"American Typewriter" size:rDetailFontSize]; } - (void)bindAnimal:(Animal *)animal { if (_nameText != animal.name) { _nameText = animal.name; } if (_detailText != animal.detail) { _detailText = animal.detail; } if (_imageName != animal.imageName) { _imageName = animal.imageName; } [self setNeedsDisplay]; } - (void)drawContentView:(CGRect)rect { static UIColor *nameColor; nameColor = [UIColor blackColor]; static UIColor *detailColor; detailColor = [UIColor darkGrayColor]; CGContextRef context = UIGraphicsGetCurrentContext(); CGRect cellRect = self.frame; if (self.highlighted || self.selected) { CGContextSetFillColorWithColor(context, [UIColor lightGrayColor].CGColor); CGContextFillRect(context, CGRectMake(0, 0, cellRect.size.width, cellRect.size.height)); } else { CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor); CGContextFillRect(context, CGRectMake(0, 0, cellRect.size.width, cellRect.size.height)); } UIImage *image = [UIImage imageNamed:_imageName]; [image drawInRect:CGRectMake(5, 5, 50, 50)]; [nameColor set]; [_nameText drawAtPoint:CGPointMake(65, 10) forWidth:200 withFont:NameFont fontSize:rNameFontSize lineBreakMode:NSLineBreakByWordWrapping baselineAdjustment:UIBaselineAdjustmentAlignBaselines]; [detailColor set]; [_detailText drawAtPoint:CGPointMake(180, 40) forWidth:120 withFont:DetailFont fontSize:rDetailFontSize lineBreakMode:NSLineBreakByWordWrapping baselineAdjustment:UIBaselineAdjustmentAlignBaselines]; }
#import @interface ViewController : UITableViewController @end
#import "ViewController.h" #import "Animal.h" #import "HRCustomCell.h" #define rAnimalCount 100 #define rRowHeight 60 @interface ViewController () { NSMutableArray *_animalList; } @end static NSString * const CellIdentifier = @"HRCell"; @implementation ViewController - (void)loadAnimals { _animalList = [NSMutableArray arrayWithCapacity:rAnimalCount]; for (NSInteger i = 0; i < rAnimalCount; i++) { Animal *animal = [[Animal alloc] init]; NSString *name = [NSString stringWithFormat:@"Animal-%03d", i+1]; NSString *detail = [NSString stringWithFormat:@"dog or cat?"]; NSInteger seed = arc4random()%8 + 1; NSString *imageName = [NSString stringWithFormat:@"head%02d", seed+1]; animal.name = name; animal.detail = detail; animal.imageName = imageName; [_animalList addObject:animal]; } } - (void)viewDidLoad { [super viewDidLoad]; [self loadAnimals]; [self.tableView registerClass:[HRCustomCell class] forCellReuseIdentifier:CellIdentifier]; self.title = @"Animals"; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return rRowHeight; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _animalList.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { HRCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; [cell bindAnimal:_animalList[indexPath.row]]; return cell; } @end
如对本文有疑问, 点击进行留言回复!!
Codeforces Round #657 (Div. 2) C题
hdu2222 Keywords Search(ac自动机模板-kuangbin)
IOS开发控件视图day08:监听文本框状态设置按钮状态不可点击且变灰
网友评论