2015-11-10 MVC

自定义Cell

TechNewsCell.h

#import <UIKit/UIKit.h>
@class TechModel;


@interface TechNewsCell : UITableViewCell
@property (nonatomic, strong) TechModel *techModel;
/** 提供一个类方法,可以快速创建 Cell */
+ (instancetype)cellWithTableView:(UITableView *)tableView;
@end

 

TechNewsCell.m

#import "TechNewsCell.h"
#import "TechModel.h"
#import "UIImageView+WebCache.h"

@interface TechNewsCell ()
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UIImageView *picImage;
@end

@implementation TechNewsCell
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    // 1. 可重用标示符
    static NSString *ID = @"Cell";
    // 2. tableView查询可重用Cell
    TechNewsCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    // 3. 如果没有可重用cell
    if (cell == nil) {
        NSLog(@"加载XIB");
        // 从XIB加载自定义视图
        cell = [[[NSBundle mainBundle] loadNibNamed:@"TechNewsCell" owner:nil options:nil] lastObject];
    }
    return cell;
}

- (void)setTechModel:(TechModel *)techModel
{
    _techModel = techModel;
    self.timeLabel.text = techModel.time;
    self.titleLabel.text = techModel.title;
    [self.picImage sd_setImageWithURL:[NSURL URLWithString:techModel.picUrl]];
}

Model类

#import <Foundation/Foundation.h>
@interface TechModel : NSObject
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *time;
@property (nonatomic, copy) NSString *picUrl;
@property (nonatomic, copy) NSString *url;
@end

 

Controller控制器

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.techNewsArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TechNewsCell *cell = [TechNewsCell cellWithTableView:tableView];
    cell.techModel = self.techNewsArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}

posted @ 2015-11-10 20:29  家丁林三  阅读(120)  评论(0)    收藏  举报