欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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