IOS开发基础之微博项目

IOS开发基础之微博项目

IOS开发基础之微博项目_第1张图片
IOS开发基础之微博项目_第2张图片
IOS开发基础之微博项目_第3张图片

关键性代码

//
//  NJViewController.m
//  06-预习-微博(通过代码自定义cell)

//
#import "NJViewController.h"
#import "NJWeiboCell.h"
#import "NJWeibo.h"
#import "NJWeiboFrame.h"

@interface NJViewController ()
@property (nonatomic, strong) NSMutableArray *weiboFrames;
@end

@implementation NJViewController

- (NSMutableArray *)weiboFrames
{
     
    if (_weiboFrames == nil) {
     
        // 1.加载数据
        NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"weibo.plist" ofType:nil]];
        
        // 2.字典 转成 模型
        _weiboFrames = [NSMutableArray array];
        for (NSDictionary *dict in array) {
     
            // 创建frame对象
            NJWeiboFrame *weiboF = [[NJWeiboFrame alloc] init];
            weiboF.weibo = [NJWeibo weiboWithDict:dict];
            
            [_weiboFrames addObject:weiboF];
        }
    }
    return _weiboFrames;
}

- (BOOL)prefersStatusBarHidden
{
     
    return YES;
}

#pragma mark - 数据源方法
#pragma mark 一共有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
     
    return self.weiboFrames.count;
}

#pragma mark 每一行显示怎样的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     
    // 1.去缓存池中取出cell
    static NSString *ID = @"weibo";
    NJWeiboCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    // 2.缓存池没有cell,重新创建cell
    if (cell == nil) {
     
        cell = [[NJWeiboCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    // 3.传递模型数据
    cell.weiboFrame = self.weiboFrames[indexPath.row];
    
    return cell;
}

#pragma mark - 代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
     
    return [self.weiboFrames[indexPath.row] cellHeight];
}

@end

//
//  NJWeiboCell.h
//  06-预习-微博(通过代码自定义cell)
//

//

#import 
@class NJWeiboFrame;

@interface NJWeiboCell : UITableViewCell
@property (nonatomic, strong) NJWeiboFrame *weiboFrame;
@end

//
//  NJWeiboCell.m
//  06-预习-微博(通过代码自定义cell)
//

//

#import "NJWeiboCell.h"
#import "NJWeibo.h"
#import "NJWeiboFrame.h"

@interface NJWeiboCell()
// 1.头像
@property (nonatomic, weak) UIImageView *iconView;
// 2.昵称
@property (nonatomic, weak) UILabel *nameLabel;
// 3.会员图标
@property (nonatomic, weak) UIImageView *vipView;
// 4.正文
@property (nonatomic, weak) UILabel *textView;
// 5.配图
@property (nonatomic, weak) UIImageView *pictureView;
@end

@implementation NJWeiboCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
     
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
     
        // 添加内部的子控件
        
        // 1.头像
        UIImageView *iconView = [[UIImageView alloc] init];
        [self.contentView addSubview:iconView];
        self.iconView = iconView;
        
        // 2.昵称
        UILabel *nameLabel = [[UILabel alloc] init];
        nameLabel.font = kNameFont;
        [self.contentView addSubview:nameLabel];
        self.nameLabel = nameLabel;
        
        // 3.会员图标
        UIImageView *vipView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"vip.png"]];
        [self.contentView addSubview:vipView];
        self.vipView = vipView;
        
        // 4.微博正文
        UILabel *textView = [[UILabel alloc] init];
        textView.font = kTextFont;
        textView.numberOfLines = 0; // 自动换行
        [self.contentView addSubview:textView];
        self.textView = textView;
        
        // 5.配图
        UIImageView *pictureView = [[UIImageView alloc] init];
        [self.contentView addSubview:pictureView];
        self.pictureView = pictureView;
    }
    return self;
}

- (void)setWeiboFrame:(NJWeiboFrame *)weiboFrame
{
     
    _weiboFrame = weiboFrame;
    
    // 1.设置微博数据
    [self settingData];
    
    // 2.设置子控件的frame(x、y、width、height)
    [self settingSubviewFrame];
}

#pragma mark 设置子控件的frame
- (void)settingSubviewFrame
{
     
    // 1.头像
    self.iconView.frame = self.weiboFrame.iconF;
    
    // 2.昵称
    self.nameLabel.frame = self.weiboFrame.nameF;
    
    // 3.vip
    self.vipView.frame = self.weiboFrame.vipF;
    
    // 4.正文
    self.textView.frame = self.weiboFrame.textF;
    
    // 5.配图
    if (self.weiboFrame.weibo.picture) {
      // 有配图
        self.pictureView.frame = self.weiboFrame.pictureF;
    }
}

#pragma mark 设置微博数据
- (void)settingData
{
     
    NJWeibo *weibo = self.weiboFrame.weibo;
    
    // 1.头像
    self.iconView.image = [UIImage imageNamed:weibo.icon];
    
    // 2.昵称
    self.nameLabel.text = weibo.name;
    if (weibo.vip) {
     
        self.nameLabel.textColor = [UIColor redColor];
    } else {
     
        self.nameLabel.textColor = [UIColor blackColor];
    }
    
    // 3.会员图标
    self.vipView.hidden = !weibo.vip;
    
    // 4.正文
    self.textView.text = weibo.text;
    
    // 5.配图
    if (weibo.picture) {
      // 有配图
        self.pictureView.hidden = NO;
        
        self.pictureView.image = [UIImage imageNamed:weibo.picture];
    } else {
      // 没有配图
        self.pictureView.hidden = YES;
    }
}
@end

//
//  NJWeibo.h
//  06-预习-微博(通过代码自定义cell)
//

//

#import 

@interface NJWeibo : NSObject
@property (nonatomic, copy) NSString *text; // 内容
@property (nonatomic, copy) NSString *icon; // 头像
@property (nonatomic, copy) NSString *name; // 昵称
@property (nonatomic, copy) NSString *picture; // 配图
@property (nonatomic, assign) BOOL vip;

- (id)initWithDict:(NSDictionary *)dict;
+ (id)weiboWithDict:(NSDictionary *)dict;
@end

//
//  NJWeibo.m
//  06-预习-微博(通过代码自定义cell)
//
//  Created by 李南江 on 14-4-21.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import "NJWeibo.h"

@implementation NJWeibo

- (id)initWithDict:(NSDictionary *)dict
{
     
    if (self = [super init]) {
     
        self.name = dict[@"name"];
        self.vip = [dict[@"vip"] boolValue];
        self.picture = dict[@"picture"];
        self.icon = dict[@"icon"];
        self.text = dict[@"text"];
    }
    return self;
}

+ (id)weiboWithDict:(NSDictionary *)dict
{
     
    return [[self alloc] initWithDict:dict];
}

@end

//
//  NJWeiboFrame.h
//  06-预习-微博(通过代码自定义cell)
//

//

#define kNameFont [UIFont systemFontOfSize:15]
#define kTextFont [UIFont systemFontOfSize:15]

#import 
@class NJWeibo;

@interface NJWeiboFrame : NSObject

@property (nonatomic, assign, readonly) CGRect iconF;
@property (nonatomic, assign, readonly) CGRect nameF;
@property (nonatomic, assign, readonly) CGRect vipF;
@property (nonatomic, assign, readonly) CGRect textF;
@property (nonatomic, assign, readonly) CGRect pictureF;

@property (nonatomic, assign, readonly) CGFloat cellHeight;

@property (nonatomic, strong) NJWeibo *weibo;

@end

//
//  NJWeiboFrame.m
//  06-预习-微博(通过代码自定义cell)
//
//  Created by 李南江 on 14-4-21.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

// cell的边框宽度
#define kCellBorder 10
// 头像的宽高
#define kIconWH 30
// vip的宽高
#define kVipWH 14
// 图片尺寸
#define kImageWH 70

#import "NJWeiboFrame.h"
#import "NJWeibo.h"

@implementation NJWeiboFrame
- (void)setWeibo:(NJWeibo *)weibo
{
     
    _weibo = weibo;
    
    // 1.头像
    CGFloat iconX = kCellBorder;
    CGFloat iconY = kCellBorder;
    _iconF = CGRectMake(iconX, iconY, kIconWH, kIconWH);
    
    // 2.昵称
    // 计算用户名称的尺寸
    CGSize nameSize = [_weibo.name sizeWithFont:kNameFont];
    CGFloat nameX = CGRectGetMaxX(_iconF) + kCellBorder;
    CGFloat nameY = iconY + (kIconWH - nameSize.height) * 0.5;
    _nameF = CGRectMake(nameX, nameY, nameSize.width, nameSize.height);
    
    // 3.vip
    CGFloat vipX = CGRectGetMaxX(_nameF) + kCellBorder;
    CGFloat vipY = nameY;
    _vipF = CGRectMake(vipX, vipY, kVipWH, kVipWH);
    
    // 4.正文
    CGFloat textX = iconX;
    CGFloat textY = CGRectGetMaxY(_iconF) + kCellBorder;
    CGFloat textW = 320 - 2 * kCellBorder;
    // 计算文字尺寸(显示文字的宽度)
    CGSize textSize = [_weibo.text sizeWithFont:kTextFont constrainedToSize:CGSizeMake(textW, MAXFLOAT)];
    
    _textF = CGRectMake(textX, textY, textW, textSize.height);
    
    // 5.配图\计算cell的高度
    if (_weibo.picture) {
      // 有配图
        CGFloat pictureX = textX;
        CGFloat pictureY = CGRectGetMaxY(_textF) + kCellBorder;
        _pictureF = CGRectMake(pictureX, pictureY, kImageWH, kImageWH);
        
        _cellHeight = CGRectGetMaxY(_pictureF) + kCellBorder;
    } else {
      // 没有配图
        _cellHeight = CGRectGetMaxY(_textF) + kCellBorder;
    }
}
@end

你可能感兴趣的:(IOS,ios)