iOS 绘制 cell --- 新手学习笔记

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.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 <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;
}

@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




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值