網(wǎng)易新聞頻道標簽

版本記錄

版本號 時間
V1.0 2017.06.04

前言

網(wǎng)易新聞是門戶網(wǎng)站,和今日頭條類似膀估,都是標簽頻道,每個頻道都有對應(yīng)的新聞,這一篇主要就是模擬網(wǎng)易新聞轴或。

網(wǎng)易新聞圖例

下面還是先看一下網(wǎng)易新聞的幾個截圖。

圖1
圖2

網(wǎng)易新聞模擬實現(xiàn)

下面我們先看一下代碼組織結(jié)構(gòu)仰禀。

網(wǎng)易新聞代碼組織結(jié)構(gòu)

下面直接看代碼

1. JJHomeVC.h

#import <UIKit/UIKit.h>

@interface JJHomeVC : UIViewController

@end
2. JJHomeVC.m

#import "JJHomeVC.h"
#import "JJFlowLayout.h"
#import "JJChannelModel.h"
#import "JJChannelLabel.h"
#import "JJCollectionViewCell.h"

@interface JJHomeVC () <UIScrollViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UIScrollView *channelScrollView;
@property (nonatomic, strong) UICollectionView *newsCollectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *collectionLayout;
@property (nonatomic, strong) NSArray *channelArr;
@property (nonatomic, strong) NSMutableArray <JJChannelLabel *>*channelLabelArrM;

@end

@implementation JJHomeVC

static NSString * const kJJHomeVCCollectionViewReuserIdentify = @"kJJHomeVCCollectionViewReuserIdentify";

#pragma mark - Override Base Function

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"網(wǎng)易新聞";
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.channelLabelArrM = [NSMutableArray array];
    [self setupUI];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self createChannelLabels];
    NSLog(@"%@----",self.channelArr);
}

#pragma mark - Object Private Function

- (void)createChannelLabels
{
    self.channelArr = [JJChannelModel gainChannelArr];
    
    CGFloat labelWidth = 80.0;
    CGFloat lableHeight = self.channelScrollView.bounds.size.height;
    
    for (NSInteger i = 0; i < self.channelArr.count; i++) {
        JJChannelLabel *label = [[JJChannelLabel alloc] init];
        [self.channelScrollView addSubview:label];
        CGFloat labelMinX = i * labelWidth;
        label.frame = CGRectMake(labelMinX, 0.0, labelWidth, lableHeight);
        
        JJChannelModel *model = self.channelArr[i];
        label.text = model.tname;
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDidTapped:)];
        [label addGestureRecognizer:tapGesture];
        label.userInteractionEnabled = YES;
        label.tag = i;
        [self.channelLabelArrM addObject:label];
        
        if (i == 0) {
            label.scale = 1.0;  //默認第一個為最大的縮放比
        }
    }
    
    self.channelScrollView.contentSize = CGSizeMake(labelWidth * self.channelArr.count, 0);

}

- (void)setupUI
{
    //scrollView
    UIScrollView *channelScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 64.0, kJJCommonScreenWidth, 80.0)];
    channelScrollView.backgroundColor = [UIColor whiteColor];
    channelScrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:channelScrollView];
    self.channelScrollView = channelScrollView;
    
    //collectionView
    JJFlowLayout *layout = [[JJFlowLayout alloc] init];
    UICollectionView *newsCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0.0, 144.0, kJJCommonScreenWidth, kJJCommonScreenHeight - 144.0) collectionViewLayout:layout];
    newsCollectionView.pagingEnabled = YES;
    [newsCollectionView registerClass:[JJCollectionViewCell class] forCellWithReuseIdentifier:kJJHomeVCCollectionViewReuserIdentify];
    newsCollectionView.delegate = self;
    newsCollectionView.dataSource = self;
    [self.view addSubview:newsCollectionView];
    self.newsCollectionView = newsCollectionView;
    
}

#pragma mark - Action && Notification

- (void)tapDidTapped:(UITapGestureRecognizer *)tapGesture
{
    JJChannelLabel *selectedChannelLabel = (JJChannelLabel *)tapGesture.view;
    
    //計算選中標簽滾動到居中時,需要的偏移量
    CGFloat offsetX = selectedChannelLabel.center.x - (self.view.bounds.size.width * 0.5);
    //設(shè)置最大和最小滾動的臨界值
    CGFloat minOffset = 0;
    CGFloat maxOffset = self.channelScrollView.contentSize.width - self.view.bounds.size.width;
    //判斷是否超出了最大和最小滾動范圍
    if (offsetX < minOffset) {
        offsetX = minOffset;
    }
    if(offsetX > maxOffset){
        offsetX = maxOffset;
    }
    
    //把channelScrollView滾動到指定的位置
    [self.channelScrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
    
    //把新聞滾動視圖滾動到指定的位置
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:selectedChannelLabel.tag inSection:0];
    [self.newsCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    
    NSInteger index = selectedChannelLabel.tag;
    
    // 需要把選中的標簽設(shè)置最大的縮放比,非選中的還原
    for (NSInteger i = 0; i < self.channelLabelArrM.count; i++) {
        JJChannelLabel *label  = self.channelLabelArrM[i];
        if (index == i) {
            label.scale = 1.0;
        }
        else {
            label.scale = 0.0;
        }
    }
}

#pragma mark - UIScrollViewDelegate

//底部滾動視圖聯(lián)動頻道標簽
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //獲取新聞滾動視圖的偏移量,然后簡介獲取滾動的到index
    NSInteger index = scrollView.contentOffset.x/kJJCommonScreenWidth;
    
    //拿著新聞滾動視圖是滾動時,獲取的index,去標簽數(shù)組里面找對應(yīng)的標簽,計算居中時需要滾動的偏移量
    JJChannelLabel *selectedLabel = self.channelLabelArrM[index];
    
    //計算選中標簽滾動到居中時,需要的偏移量
    CGFloat offsetX = selectedLabel.center.x - (self.view.bounds.size.width * 0.5);
    //設(shè)置最大和最小滾動的臨界值
    CGFloat minOffset = 0;
    CGFloat maxOffset = self.channelScrollView.contentSize.width - self.view.bounds.size.width;
    //判斷是否超出了最大和最小滾動范圍
    if (offsetX < minOffset) {
        offsetX = minOffset;
    }
    else if(offsetX > maxOffset){
        offsetX = maxOffset;
    }
    [self.channelScrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}

//頻道標簽的縮放
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //獲取偏移量
    CGFloat indexOffsetX = scrollView.contentOffset.x / self.view.bounds.size.width;
    
    //滾動的索引
    NSInteger index = scrollView.contentOffset.x / self.view.bounds.size.width;
    
    //偏移百分比
    CGFloat percent = indexOffsetX - index;
    
    //要實現(xiàn)標簽的縮放,需要計算四個值:左邊標簽索引,右邊標簽索引,左邊標簽縮放比,右邊標簽縮放比
    CGFloat rightScale = percent;
    CGFloat leftScale = 1 - rightScale;
    NSInteger leftIndex = index;
    NSInteger rightIndex = leftIndex + 1;

    
    //獲取左邊的標簽
    JJChannelLabel *leftChannelLabel = self.channelLabelArrM[leftIndex];
    leftChannelLabel.scale = leftScale;
    
    //獲取右邊的標簽:防止腳標越界崩潰
    if (rightIndex < self.channelLabelArrM.count) {
        JJChannelLabel *rightLabel = self.channelLabelArrM[rightIndex];
        rightLabel.scale = rightScale;
    }
}

#pragma mark - UICollectionViewDataSource && UITableViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.channelArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    JJCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kJJHomeVCCollectionViewReuserIdentify forIndexPath:indexPath];
    JJChannelModel *model = self.channelArr[indexPath.item];
    NSString *URLStr = [NSString stringWithFormat:@"article/list/%@/0-20.html",model.tid];
    cell.URLStr = URLStr;
    return cell;
}

@end
3. JJChannelModel.h

#import <Foundation/Foundation.h>

@interface JJChannelModel : NSObject

@property (nonatomic, copy) NSString *tname;
@property (nonatomic, copy) NSString *tid;

+ (NSArray *)gainChannelArr;

@end
4. JJChannelModel.m

#import "JJChannelModel.h"

@implementation JJChannelModel

#pragma mark - Override Base Function

- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
    //這里什么都不做
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"%@ -- %@",self.tname,self.tid];
}

#pragma mark - Class Private Function

+ (instancetype)channelModelWithDict:(NSDictionary *)dict
{
    JJChannelModel *model = [[JJChannelModel alloc] init];
    [model setValuesForKeysWithDictionary:dict];
    return model;
}

#pragma mark - Class Public Function

+ (NSArray *)gainChannelArr
{
    NSString *filePathStr = [[NSBundle mainBundle] pathForResource:@"topic_news.json"  ofType:nil];
    NSData *jsonData = [NSData dataWithContentsOfFile:filePathStr];
    NSDictionary *resultDict = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:NULL];
    NSArray *contentArr = [resultDict objectForKey:@"tList"];
    NSMutableArray *contentArrM = [NSMutableArray arrayWithCapacity:contentArr.count];
    [contentArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        JJChannelModel *model = [JJChannelModel channelModelWithDict:obj];
        [contentArrM addObject:model];
    }];
    
    //按照tid從小到大排序
    [contentArrM sortUsingComparator:^NSComparisonResult(JJChannelModel* obj1,JJChannelModel* obj2) {
        return [obj1.tid compare:obj2.tid];
    }];

    return contentArrM.copy;
}

@end

5. JJNewsModel.h

#import <Foundation/Foundation.h>

@interface JJNewsModel : NSObject

@property (nonatomic,copy) NSString *title;             // 新聞標題
@property (nonatomic,copy) NSString *imgsrc;            // 新聞圖標
@property (nonatomic,copy) NSString *source;            // 新聞來源
@property (nonatomic,strong) NSNumber *replyCount;      // 新聞回復數(shù)
@property (nonatomic, strong) NSArray *imgextra;        // 多張配圖
@property (nonatomic, assign) BOOL imgType;             // 大圖標記

@end
6. JJNewsModel.m

#import "JJNewsModel.h"

@implementation JJNewsModel

- (NSString *)description
{
    return [NSString stringWithFormat:@"%@-%@-%@",self.title,self.source,self.replyCount];
}

@end
7. JJNetworkTool.h

#import <AFNetworking/AFNetworking.h>

@interface JJNetworkTool : AFHTTPSessionManager

/// 單例的全局訪問點
+ (instancetype)sharedTool;

/**
 封裝的網(wǎng)絡(luò)請求工具類的GET方法
 
 @param URLString  請求的地址
 @param parameters 請求的參數(shù)
 @param success    成功的回調(diào)
 @param failed     失敗的回調(diào)
 */
- (void)GETWithURLString:(NSString *)URLString parameters:(id)parameters success:(void(^)(id responseObject))success failed:(void(^)(NSError *error))failed;

@end

8. JJNetworkTool.m

#import "JJNetworkTool.h"

@implementation JJNetworkTool

#pragma mark - Class Public Function

+ (instancetype)sharedTool
{
    static JJNetworkTool *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        NSURL *baseURL = [NSURL URLWithString:@"http://c.m.163.com/nc/"];
        instance = [[self alloc] initWithBaseURL:baseURL];
        // 增加AFN支持的文件類型
        instance.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/plain", nil];
    });
    return instance;
}

#pragma mark - Object Private Function

// AFHTTPSessionManager發(fā)送GET請求
- (void)GETWithURLString:(NSString *)URLString parameters:(id)parameters success:(void (^)(id))success failed:(void (^)(NSError *))failed
{
    [self GET:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if (success) {
            success(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failed) {
            failed(error);
        }
    }];
}

@end
9. JJFlowLayout.h

#import <UIKit/UIKit.h>

@interface JJFlowLayout : UICollectionViewFlowLayout

@end

10. JJFlowLayout.m
#import "JJFlowLayout.h"

@implementation JJFlowLayout

- (void)prepareLayout
{
    [super prepareLayout];
    
    self.itemSize = CGSizeMake(kJJCommonScreenWidth, kJJCommonScreenHeight - 144.0);
    self.minimumLineSpacing = 0.0;
    self.minimumInteritemSpacing = 0.0;
    self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
}

@end

11. JJChannelLabel.h

#import <UIKit/UIKit.h>

@interface JJChannelLabel : UILabel

@property (nonatomic, assign) CGFloat scale;    //縮放比 0.0~1.0

@end
12. JJChannelLabel.m

#import "JJChannelLabel.h"

@implementation JJChannelLabel

#pragma mark - Override Base Function

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

#pragma mark - Object Private Function

- (void)setupUI
{
    self.textAlignment = NSTextAlignmentCenter;
    self.font = [UIFont systemFontOfSize:16.0];
}

#pragma mark - Getter && Setter

- (void)setScale:(CGFloat)scale
{
    _scale = scale;
    
    //標簽漸變色 黑---紅
    self.textColor = [UIColor colorWithRed:scale green:0 blue:0 alpha:1.0];
    
    //標簽縮放
    CGFloat minScale = 1.0;
    CGFloat maxScale = 1.5;
    scale = minScale + scale * (maxScale - minScale);
    self.transform = CGAffineTransformMakeScale(scale, scale);
    
}

@end

13. JJCollectionViewCell.h

#import <UIKit/UIKit.h>

@interface JJCollectionViewCell : UICollectionViewCell

@property (nonatomic, copy) NSString *URLStr;

@end
14. JJCollectionViewCell.m

#import "JJCollectionViewCell.h"
#import "JJThreeMiddlePictureCell.h"
#import "JJOneBigPictureCell.h"
#import "JJOneSmallPictureCell.h"
#import "JJNewsModel.h"
#import "JJNetworkTool.h"

@interface JJCollectionViewCell () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *newsTableView;
@property (nonatomic, strong) NSMutableArray *newsListArrM;

@end

@implementation JJCollectionViewCell

static NSString * const kJJOneBigPictureCellReuseIdentify = @"kJJOneBigPictureCellReuseIdentify";
static NSString * const kJJOneSmallPictureCellReuseIdentify = @"kJJJOneSmallPictureCellReuseIdentify";
static NSString * const kJJThreeMiddlePictureCellReuseIdentify = @"kJJThreeMiddlePictureCellReuseIdentify";

#pragma mark - Override Base Function

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
        self.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1];//隨機色
        self.newsListArrM = [NSMutableArray array];
    }
    return self;
}

#pragma mark - Object Private Function

- (void)setupUI
{
    //tableView
    UITableView *newsTableView = [[UITableView alloc] initWithFrame:self.frame];
    newsTableView.delegate = self;
    newsTableView.dataSource = self;
    [newsTableView registerClass:[JJOneBigPictureCell class] forCellReuseIdentifier:kJJOneBigPictureCellReuseIdentify];
    [newsTableView registerClass:[JJThreeMiddlePictureCell class] forCellReuseIdentifier:kJJThreeMiddlePictureCellReuseIdentify];
    [newsTableView registerClass:[JJOneSmallPictureCell class] forCellReuseIdentifier:kJJOneSmallPictureCellReuseIdentify];
    [self.contentView addSubview:newsTableView];
    self.newsTableView = newsTableView;
}

#pragma mark - Setter && Getter

- (void)setURLStr:(NSString *)URLStr
{
    _URLStr = URLStr;
    
    [[JJNetworkTool sharedTool] GETWithURLString:URLStr parameters:nil success:^(NSDictionary *responseObject) {
        
        NSString *key = responseObject.keyEnumerator.nextObject;
        NSArray *dictArr = responseObject[key];
        NSArray *listArr = [NSArray yy_modelArrayWithClass:[JJNewsModel class] json:dictArr];
        [self.newsListArrM addObjectsFromArray:listArr];
        [self.newsTableView reloadData];
        
    } failed:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

#pragma mark - UITableViewDelegate && UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.newsListArrM.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JJNewsModel *newModel = self.newsListArrM[indexPath.row];
    if (newModel.imgType == YES) {
        JJOneBigPictureCell *cell = [tableView dequeueReusableCellWithIdentifier:kJJOneBigPictureCellReuseIdentify forIndexPath:indexPath];
        cell.newsModel = newModel;
        return cell;
    }
    else if(newModel.imgextra.count == 2) {
        JJThreeMiddlePictureCell *cell = [tableView dequeueReusableCellWithIdentifier:kJJThreeMiddlePictureCellReuseIdentify forIndexPath:indexPath];
        cell.newsModel = newModel;
        return cell;
    }
    else {
        JJOneSmallPictureCell *cell = [tableView dequeueReusableCellWithIdentifier:kJJOneSmallPictureCellReuseIdentify forIndexPath:indexPath];
        cell.newsModel = newModel;
        return cell;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JJNewsModel *newModel = self.newsListArrM[indexPath.row];
    if (newModel.imgType == YES) {
        return  180.0;
    }
    else if (newModel.imgextra.count == 2) {
        return 140.0;
    }
    return 100.0;
}

@end

15. JJOneSmallPictureCell.h

#import <UIKit/UIKit.h>

@class JJNewsModel;

@interface JJOneSmallPictureCell : UITableViewCell

@property (nonatomic, strong) JJNewsModel *newsModel;

@end

16. JJOneSmallPictureCell.m
#import "JJOneSmallPictureCell.h"
#import "JJNewsModel.h"

@interface JJOneSmallPictureCell ()

@property (nonatomic, strong) UIImageView *picImageView;
@property (nonatomic, strong) UILabel *desclabel;
@property (nonatomic, strong) UILabel *sourceLabel;
@property (nonatomic, strong) UILabel *numLabel;

@end

@implementation JJOneSmallPictureCell

#pragma mark - Override Base Function

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    //新聞圖片
    [self.picImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.width.height.equalTo(@80);
        make.centerY.equalTo(self);
        make.left.equalTo(self).offset(10.0);
    }];
    
//    self.picImageView.frame = CGRectMake(10.0, CGRectGetMidY(self.frame), 80, 80);
    
    //新聞簡介
    [self.desclabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.picImageView);
        make.left.equalTo(self.picImageView.mas_right).offset(6.0);
        make.width.equalTo(@(kJJCommonScreenWidth - 80 - 10 - 10 - 6));
        make.height.equalTo(@15);
    }];
//    self.desclabel.frame = CGRectMake(CGRectGetMaxX(self.picImageView.frame) + 6.0, CGRectGetMinY(self.picImageView.frame), kJJCommonScreenWidth - 80 - 10 - 10 - 6, 15);
    
    //新聞來源
    [self.sourceLabel sizeToFit];
    [self.sourceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.picImageView);
        make.left.equalTo(self.desclabel);
    }];
    
//    self.sourceLabel.frame = CGRectMake(CGRectGetMinX(self.desclabel.frame), CGRectGetMaxY(self.picImageView.frame) - CGRectGetHeight(self.sourceLabel.frame), CGRectGetWidth(self.sourceLabel.frame), CGRectGetHeight(self.sourceLabel.frame));
    
    //新聞評論數(shù)
    [self.numLabel sizeToFit];
    [self.numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(- 10.0);
        make.bottom.equalTo(self.sourceLabel);
    }];
//    self.numLabel.frame = CGRectMake(CGRectGetWidth(self.frame) - 10.0 - CGRectGetWidth(self.numLabel.frame), CGRectGetMinY(self.sourceLabel.frame), CGRectGetWidth(self.numLabel.frame), CGRectGetHeight(self.numLabel.frame));
}

#pragma mark - Object Private Function

- (void)setupUI
{
    self.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1];//隨機色
    
    //圖示
    UIImageView *picImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:picImageView];
    picImageView.image = [UIImage imageNamed:@"placeholderImage"];
    self.picImageView = picImageView;
    
    //新聞簡介
    UILabel *desclabel = [[UILabel alloc] init];
    desclabel.text = @"我來自火星";
    desclabel.font = [UIFont systemFontOfSize:14.0];
    [self.contentView addSubview:desclabel];
    self.desclabel = desclabel;
    
    //新聞來源
    UILabel *sourceLabel = [[UILabel alloc] init];
    sourceLabel.text = @"網(wǎng)易新聞";
    sourceLabel.font = [UIFont systemFontOfSize:14.0];
    [self.contentView addSubview:sourceLabel];
    self.sourceLabel = sourceLabel;

    //評論數(shù)
    UILabel *numLabel = [[UILabel alloc] init];
    numLabel.text = @"99999";
    numLabel.font = [UIFont systemFontOfSize:14.0];
    [self.contentView addSubview:numLabel];
    self.numLabel = numLabel;

}

#pragma mark - Getter && Setter 

- (void)setNewsModel:(JJNewsModel *)newsModel
{
    _newsModel = newsModel;
    
    [self.picImageView sd_setImageWithURL:[NSURL URLWithString:newsModel.imgsrc] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
    self.desclabel.text = newsModel.title;
    self.sourceLabel.text = newsModel.source;
    // 需要把NSNumber轉(zhuǎn)成NSString
    self.numLabel.text = [NSString stringWithFormat:@"%@",newsModel.replyCount]?:@"";
}

@end
17. JJOneBigPictureCell.h

#import <UIKit/UIKit.h>

@class JJNewsModel;

@interface JJOneBigPictureCell : UITableViewCell

@property (nonatomic, strong) JJNewsModel *newsModel;

@end
18. JJOneBigPictureCell.m

#import "JJOneBigPictureCell.h"
#import "JJNewsModel.h"

@interface JJOneBigPictureCell ()

@property (nonatomic, strong) UIImageView *picImageView;
@property (nonatomic, strong) UILabel *desclabel;
@property (nonatomic, strong) UILabel *sourceLabel;
@property (nonatomic, strong) UILabel *numLabel;

@end

@implementation JJOneBigPictureCell

#pragma mark - Override Base Function

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    //新聞圖片
    [self.picImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@120);
        make.left.equalTo(self).offset(10.0);
        make.width.equalTo(@(kJJCommonScreenWidth - 10 - 10));
        make.centerY.equalTo(self);
    }];
    
    //新聞簡介
    [self.desclabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.picImageView.mas_top).offset(-6.0);
        make.left.equalTo(self.picImageView);
        make.width.equalTo(@(kJJCommonScreenWidth - 10 - 10));
    }];
    
    //新聞來源
    [self.sourceLabel sizeToFit];
    [self.sourceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.picImageView.mas_bottom).offset(6.0);
        make.left.equalTo(self.picImageView);
    }];
    
    //新聞評論數(shù)
    [self.numLabel sizeToFit];
    [self.numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.picImageView);
        make.centerY.equalTo(self.sourceLabel);
    }];
}

#pragma mark - Object Private Function

- (void)setupUI
{
    self.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1];//隨機色
    
    //圖示
    UIImageView *picImageView = [[UIImageView alloc] init];
    picImageView.image = [UIImage imageNamed:@"placeholderImage"];
    [self.contentView addSubview:picImageView];
    self.picImageView = picImageView;
    
    //新聞簡介
    UILabel *desclabel = [[UILabel alloc] init];
    desclabel.text = @"我來自火星";
    desclabel.font = [UIFont systemFontOfSize:14.0];
    [self.contentView addSubview:desclabel];
    self.desclabel = desclabel;
    
    //新聞來源
    UILabel *sourceLabel = [[UILabel alloc] init];
    sourceLabel.text = @"網(wǎng)易新聞";
    sourceLabel.font = [UIFont systemFontOfSize:14.0];
    [self.contentView addSubview:sourceLabel];
    self.sourceLabel = sourceLabel;
    
    //評論數(shù)
    UILabel *numLabel = [[UILabel alloc] init];
    numLabel.text = @"99999";
    numLabel.font = [UIFont systemFontOfSize:14.0];
    [self.contentView addSubview:numLabel];
    self.numLabel = numLabel;
}

#pragma mark - Getter && Setter

- (void)setNewsModel:(JJNewsModel *)newsModel
{
    _newsModel = newsModel;
    
    [self.picImageView sd_setImageWithURL:[NSURL URLWithString:newsModel.imgsrc] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
    self.desclabel.text = newsModel.title;
    self.sourceLabel.text = newsModel.source;
    self.numLabel.text = [NSString stringWithFormat:@"%@",newsModel.replyCount]?:@"";

}

@end
19. JJThreeMiddlePictureCell.h

#import <UIKit/UIKit.h>

@class JJNewsModel;

@interface JJThreeMiddlePictureCell : UITableViewCell

@property (nonatomic, strong) JJNewsModel *newsModel;

@end
20. JJThreeMiddlePictureCell.m

#import "JJThreeMiddlePictureCell.h"
#import "JJNewsModel.h"

@interface JJThreeMiddlePictureCell ()

@property (nonatomic, strong) UIImageView *picImageViewLeft;
@property (nonatomic, strong) UIImageView *picImageViewMiddle;
@property (nonatomic, strong) UIImageView *picImageViewRight;
@property (nonatomic, strong) UILabel *desclabel;
@property (nonatomic, strong) UILabel *sourceLabel;
@property (nonatomic, strong) UILabel *numLabel;

@end

@implementation JJThreeMiddlePictureCell

#pragma mark - Override Base Function

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat marginWidth = (kJJCommonScreenWidth - 10 * 4)/3;
    
    //左邊圖片
    [self.picImageViewLeft mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(10.0);
        make.centerY.equalTo(self);
        make.height.equalTo(@80);
        make.width.equalTo(@(marginWidth));
    }];
    
    //中間圖片
    [self.picImageViewMiddle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.picImageViewLeft);
        make.left.equalTo(self.picImageViewLeft.mas_right).offset(10.0);
        make.height.equalTo(self.picImageViewLeft);
        make.width.equalTo(@(marginWidth));

    }];
    
    //右邊圖片
    [self.picImageViewRight mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.picImageViewLeft);
        make.left.equalTo(self.picImageViewMiddle.mas_right).offset(10.0);
        make.width.height.equalTo(self.picImageViewLeft);
        make.width.equalTo(@(marginWidth));
    }];

    
    //新聞簡介
    [self.desclabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.picImageViewLeft.mas_top).offset(-10.0);
        make.left.equalTo(self.picImageViewLeft);
        make.width.equalTo(@(kJJCommonScreenWidth - 10 - 10));
    }];
    
    //新聞來源
    [self.sourceLabel sizeToFit];
    [self.sourceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.picImageViewLeft.mas_bottom).offset(10.0);
        make.left.equalTo(self.picImageViewLeft);
    }];
    
    //新聞評論數(shù)
    [self.numLabel sizeToFit];
    [self.numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.picImageViewRight);
        make.centerY.equalTo(self.sourceLabel);
    }];
}

#pragma mark - Object Private Function

- (void)setupUI
{
    self.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1];//隨機色
    
    //左邊圖示
    UIImageView *picImageViewLeft = [[UIImageView alloc] init];
    picImageViewLeft.image = [UIImage imageNamed:@"placeholderImage"];
    [self.contentView addSubview:picImageViewLeft];
    self.picImageViewLeft = picImageViewLeft;
    
    //中間圖示
    UIImageView *picImageViewMiddle = [[UIImageView alloc] init];
    picImageViewMiddle.image = [UIImage imageNamed:@"placeholderImage"];
    [self.contentView addSubview:picImageViewMiddle];
    self.picImageViewMiddle = picImageViewMiddle;
    
    //右邊圖示
    UIImageView *picImageViewRight = [[UIImageView alloc] init];
    picImageViewRight.image = [UIImage imageNamed:@"placeholderImage"];
    [self.contentView addSubview:picImageViewRight];
    self.picImageViewRight = picImageViewRight;
    
    //新聞簡介
    UILabel *desclabel = [[UILabel alloc] init];
    desclabel.text = @"我來自火星";
    desclabel.font = [UIFont systemFontOfSize:14.0];
    [self.contentView addSubview:desclabel];
    self.desclabel = desclabel;
    
    //新聞來源
    UILabel *sourceLabel = [[UILabel alloc] init];
    sourceLabel.text = @"網(wǎng)易新聞";
    sourceLabel.font = [UIFont systemFontOfSize:14.0];
    [self.contentView addSubview:sourceLabel];
    self.sourceLabel = sourceLabel;
    
    //評論數(shù)
    UILabel *numLabel = [[UILabel alloc] init];
    numLabel.text = @"99999";
    numLabel.font = [UIFont systemFontOfSize:14.0];
    [self.contentView addSubview:numLabel];
    self.numLabel = numLabel;
}

#pragma mark - Getter && Setter

- (void)setNewsModel:(JJNewsModel *)newsModel
{
    _newsModel = newsModel;
    
    [self.picImageViewLeft sd_setImageWithURL:[NSURL URLWithString:newsModel.imgsrc] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
    self.desclabel.text = newsModel.title;
    self.sourceLabel.text = newsModel.source;
    self.numLabel.text = [NSString stringWithFormat:@"%@",newsModel.replyCount]?:@"";
    
    //中間圖片
    NSDictionary *imgDict = newsModel.imgextra[0];
    NSString *imgsrc = imgDict[@"imgsrc"];
    [self.picImageViewMiddle sd_setImageWithURL:[NSURL URLWithString:imgsrc] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
    
    //右邊圖片
    NSDictionary *imgRightDict = newsModel.imgextra[1];
    NSString *imgsrcRight = imgRightDict[@"imgsrc"];
    [self.picImageViewRight sd_setImageWithURL:[NSURL URLWithString:imgsrcRight] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
}

@end

網(wǎng)易模擬結(jié)果

下面看一下網(wǎng)易新聞的標簽滾動和顯示結(jié)果照雁。

網(wǎng)易新聞

后記

??這個只是網(wǎng)易新聞的demo,模擬標簽的滾動和新聞的顯示答恶,未完饺蚊,待續(xù)~~~

奮斗
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市悬嗓,隨后出現(xiàn)的幾起案子污呼,更是在濱河造成了極大的恐慌,老刑警劉巖包竹,帶你破解...
    沈念sama閱讀 218,451評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件燕酷,死亡現(xiàn)場離奇詭異,居然都是意外死亡周瞎,警方通過查閱死者的電腦和手機苗缩,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來声诸,“玉大人酱讶,你說我怎么就攤上這事”宋冢” “怎么了泻肯?”我有些...
    開封第一講書人閱讀 164,782評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長慰照。 經(jīng)常有香客問我软免,道長,這世上最難降的妖魔是什么焚挠? 我笑而不...
    開封第一講書人閱讀 58,709評論 1 294
  • 正文 為了忘掉前任膏萧,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘榛泛。我一直安慰自己蝌蹂,他們只是感情好,可當我...
    茶點故事閱讀 67,733評論 6 392
  • 文/花漫 我一把揭開白布曹锨。 她就那樣靜靜地躺著孤个,像睡著了一般。 火紅的嫁衣襯著肌膚如雪沛简。 梳的紋絲不亂的頭發(fā)上齐鲤,一...
    開封第一講書人閱讀 51,578評論 1 305
  • 那天,我揣著相機與錄音椒楣,去河邊找鬼给郊。 笑死,一個胖子當著我的面吹牛捧灰,可吹牛的內(nèi)容都是我干的淆九。 我是一名探鬼主播,決...
    沈念sama閱讀 40,320評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼毛俏,長吁一口氣:“原來是場噩夢啊……” “哼炭庙!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起煌寇,我...
    開封第一講書人閱讀 39,241評論 0 276
  • 序言:老撾萬榮一對情侶失蹤焕蹄,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后阀溶,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體腻脏,經(jīng)...
    沈念sama閱讀 45,686評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,878評論 3 336
  • 正文 我和宋清朗相戀三年淌哟,在試婚紗的時候發(fā)現(xiàn)自己被綠了迹卢。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,992評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡徒仓,死狀恐怖腐碱,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情掉弛,我是刑警寧澤症见,帶...
    沈念sama閱讀 35,715評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站殃饿,受9級特大地震影響谋作,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜乎芳,卻給世界環(huán)境...
    茶點故事閱讀 41,336評論 3 330
  • 文/蒙蒙 一遵蚜、第九天 我趴在偏房一處隱蔽的房頂上張望帖池。 院中可真熱鬧,春花似錦吭净、人聲如沸睡汹。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽囚巴。三九已至,卻和暖如春友扰,著一層夾襖步出監(jiān)牢的瞬間彤叉,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評論 1 270
  • 我被黑心中介騙來泰國打工村怪, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留秽浇,地道東北人。 一個月前我還...
    沈念sama閱讀 48,173評論 3 370
  • 正文 我出身青樓实愚,卻偏偏與公主長得像兼呵,于是被迫代替她去往敵國和親兔辅。 傳聞我的和親對象是個殘疾皇子腊敲,可洞房花燭夜當晚...
    茶點故事閱讀 44,947評論 2 355

推薦閱讀更多精彩內(nèi)容

  • 騰訊新聞客戶端(V5.1.5.29)產(chǎn)品體驗報告 一、概述 1维苔、體驗環(huán)境 體驗產(chǎn)品:騰訊新聞客戶端 軟件版本:5....
    村里搬磚的月野兔閱讀 2,356評論 1 15
  • 父親前幾天給我發(fā)了一個紅包碰辅,16.8元。 父親給我打電話的時候介时,剛好手上有點事在忙没宾,于是我告訴他我晚點給他回過去就...
    思潔大太陽啦啦啦閱讀 263評論 0 1
  • 我偷偷的把那些自卑,放在記憶的深處.它就像一個無惡不作的惡魔,我想盡無數(shù)辦法終于打敗它.把它封印在我最偏僻最深處的...
    Soul麥芽閱讀 1,298評論 51 49
  • 一、說明 ??筆記主要是記錄一些本人在開發(fā)當中的學習和使用筆記沸柔。筆記內(nèi)容包含一些本人覺得重要的知識點循衰、本人易犯的錯...
    lipyhui閱讀 283評論 0 1
  • 河河已經(jīng)一歲多了,目前似乎還是散養(yǎng)狀態(tài)褐澎,關(guān)于她的未來会钝,當父親的有責任仔細思考規(guī)劃。 傳統(tǒng)的方式是和我這一代相同的路...
    行動是第一生產(chǎn)力閱讀 141評論 1 0