iOS 绘制 cell --- 新手学习笔记
程序员文章站
2022-05-23 08:14:26
iOS 绘制 cell --- 新手学习笔记。
1.Animal.h 文件中
#import
@interface Animal : NSObject
@prop...
iOS 绘制 cell --- 新手学习笔记。
1.Animal.h 文件中
#import @interface Animal : NSObject @property (strong, nonatomic) NSString *name; @property (strong, nonatomic) NSString *detail; @property (strong, nonatomic) NSString *imageName; @end
2.Animal.m 文件中
#import "Animal.h" @implementation Animal @end
3.创建一个cell继承UITableViewCell ------HRCell.h
#import @interface HRCell : UITableViewCell { UIView *contentView; } - (void)drawContentView:(CGRect)rect; @end
4.创建一个自定义的cell继承 ----HRCell 的 HRCustomCell .h 文件
#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
5.HRCustomCell.m 文件
#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]; }
6.控制器中的.h 文件
#import @interface ViewController : UITableViewController @end
7.控制器中的.m文件
#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
上一篇: Java 重定向与管道
下一篇: Ubuntu开机进入busybox怎么办
推荐阅读
-
iOS bounds学习笔记以及仿写UIScrollView部分功能详解
-
ios蓝牙开发学习笔记(四)ios蓝牙应用的后台处理
-
iOS渐变字体、动态条纹、获取特定位置cell、笔记App、购物车界面等源码
-
iOS 绘制 cell --- 新手学习笔记
-
Xamarin 学习笔记 - 配置环境(Windows & iOS)
-
iOS学习笔记-139.RunLoop07——Runloop处理流程
-
iOS学习笔记-128.SDWebImage4——框架内部调用简单分析
-
iOS学习笔记-147.网络07——NSURLConnection04_简单JSON的解析
-
iOS学习笔记-143.网络03——自己搭建的后台登陆接口文档
-
iOS学习笔记-139.RunLoop08——Runloop应用之常驻线程