标签:
自定义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;
}
标签:
原文地址:http://www.cnblogs.com/CoderVan/p/4954168.html