iOS 绘制 cell --- 新手学习笔记
来源:互联网 发布:适合程序员的壁纸 编辑:程序博客网 时间:2024/05/22 17:26
1. 看的一位前辈的记下来的
1.Animal.h 文件中
#import <Foundation/Foundation.h>@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 <UIKit/UIKit.h>@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.0fstatic 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 <UIKit/UIKit.h>@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;}@endstatic 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
阅读全文
0 0
- iOS 绘制 cell --- 新手学习笔记
- tableview属性 不显示cell分割线,右边小箭头取消选中 iOS 新手笔记
- IOS学习 Cell自定义
- ios 学习笔记5cell 的选中效果消失
- iOS中UITableView学习笔记(二)之cell重用
- ios学习笔记: TableView利用label调整cell高度
- iOS学习笔记 自定义cell并且文本自适应高度
- IOS table动态cell - 笔记
- 【代码笔记】iOS-cell折叠
- ios学习--table cell定制
- ORACLE 新手学习笔记
- nginx 新手学习笔记
- Matlab新手学习笔记
- 新手git学习笔记
- linux 新手学习笔记
- iOS开发:UITableView的优化技巧-异步绘制Cell
- iOS开发:UITableView的优化技巧-异步绘制Cell
- D3D学习笔记:绘制
- idea报错:[2016-08-31 09:20:10,763] Artifact xxx:war exploded: Error during artifact deployment.
- Vue开源项目库汇总
- Fork/Join 实战中的实例
- yum使用出错“ File "/usr/bin/yum", line 30 except KeyboardInterrupt, e: SyntaxError: invalid syntax“
- 【量亿数据】量化投资的应用在未来将改变所有行业
- iOS 绘制 cell --- 新手学习笔记
- mybatis动态sql中的trim标签的使用
- C/C++的inline函数解释
- MachineLearning-DeepLearning-Neural Network
- RecylerView:no adapter attached;skipping layout
- 对RecyclerView的ViewType的使用以及添加Foot和Head的心得
- 线程互斥锁与死锁
- 适合初学者看的RxJava2教程
- javascript 常用DOM操作整理