码迷,mamicode.com
首页 > Web开发 > 详细

2015-11-10 MVC

时间:2015-11-10 20:51:38      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:

自定义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;
}

2015-11-10 MVC

标签:

原文地址:http://www.cnblogs.com/CoderVan/p/4954168.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!