美文网首页
封装轮播图(没加定时器)

封装轮播图(没加定时器)

作者: 不高冷的龙拾柒 | 来源:发表于2016-08-01 09:01 被阅读0次

    //
    // FLLView.h
    // 10-CustomCell-易车
    //
    // Created by LL.F on 16/7/27.
    // Copyright © 2016年 LL.F. All rights reserved.
    //

    import <UIKit/UIKit.h>

    @interface FLLView : UIView

    @property (nonatomic, strong) UIButton *imageButton;
    @property (nonatomic, strong) UILabel *bottomLabel;
    - (void)setWithButtonImage:(NSString *)buttonImage
              bottomLabelText:(NSString *)bottomLabelText;
    

    @end
    //
    // FLLView.m
    // 10-CustomCell-易车
    //
    // Created by LL.F on 16/7/27.
    // Copyright © 2016年 LL.F. All rights reserved.
    //

    import "FLLView.h"

    @implementation FLLView

    // 初始化
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            _imageButton = [UIButton buttonWithType:UIButtonTypeSystem];
            _imageButton.frame = CGRectMake(10, 10, 60, 60);
    //        _imageButton.backgroundColor = [UIColor redColor];
            [self addSubview:_imageButton];
            
            _bottomLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 70, 90, 30)];
            _bottomLabel.textAlignment = 1;
            _bottomLabel.font = [UIFont systemFontOfSize:15];
    //        _bottomLabel.backgroundColor = [UIColor blueColor];
            [self addSubview:_bottomLabel];
        }
        return self;
    }
    // 设置button图片和bottomLabel文字
    - (void)setWithButtonImage:(NSString *)buttonImage bottomLabelText:(NSString *)bottomLabelText
    {
    
        [_imageButton setImage:[[UIImage imageNamed:buttonImage] imageWithRenderingMode:1]forState:UIControlStateNormal];
        _bottomLabel.text = bottomLabelText;
        
    }
    

    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.

    • (void)drawRect:(CGRect)rect {
      // Drawing code
      }
      */

    @end
    //
    // ViewController.m
    // 10-CustomCell-易车
    //
    // Created by LL.F on 16/7/27.
    // Copyright © 2016年 LL.F. All rights reserved.
    //

    import "ViewController.h"

    import "FLLCycleImages.h"

    import "FLLView.h"

    import "ImageTableViewCell.h"

    import "NormolTableViewCell.h"

    import "CellModel.h"

    define kScreenbounds [UIScreen mainScreen].bounds

    define kScreenWidth [UIScreen mainScreen].bounds.size.width

    define kScreenHeight [UIScreen mainScreen].bounds.size.height

    @interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
    @property (nonatomic, strong) UITableView *tableView;
    @property (nonatomic, strong) NSMutableArray *dataSource;
    @property (nonatomic, strong) NSMutableArray *cycleArray;
    @end

    @implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        
        //1.根据提供的Plist数据完成简易版易车界面(无图片显示)
        //注意: plist文件读取出来是个NSDictionary, 打印之后根据内容解析转换成相应的类
        //2.根据提供的Plist数据完成完整版易车界面(有图片显示并调研SDWebImage)
        
        // 初始化tableview
        self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
        
        [_tableView registerClass:[ImageTableViewCell class] forCellReuseIdentifier:@"imageCell"];
        [_tableView registerClass:[NormolTableViewCell class] forCellReuseIdentifier:@"normolCell"];
        
        // 数据解析
        self.dataSource = [NSMutableArray array];
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"YiChe" ofType:@"plist"];
        NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:path];
        NSDictionary *dataDic = [dictionary objectForKey:@"data"];
        NSArray *listArray = [dataDic objectForKey:@"list"];
        for (NSDictionary *dic in listArray) {
            CellModel *model = [[CellModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataSource addObject:model];
        }
        //NSLog(@"%@", _dataSource);
        
        // 轮播图数据解析
        self.cycleArray = [NSMutableArray array];
        NSArray *cycleImageArray = [dataDic objectForKey:@"cycleImage"];
        for (NSDictionary *dic in cycleImageArray) {
            CellModel *model = [[CellModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            NSLog(@"%@", model.picCover);
            [self.cycleArray addObject:model.picCover];
        }
        [_cycleArray removeLastObject];
        //NSLog(@"%@", _cycleArray);
    
    }
    
    // cell个数
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    
        return _dataSource.count;
        
    }
    // cell内容
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (indexPath.row == 0) {
            ImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"imageCell"];
            [cell.fllView1 setWithButtonImage:@"1" bottomLabelText:@"贷款买车"];
            [cell.fllView2 setWithButtonImage:@"2" bottomLabelText:@"直销车型"];
            [cell.fllView3 setWithButtonImage:@"3" bottomLabelText:@"低价买车"];
            [cell.fllView4 setWithButtonImage:@"4" bottomLabelText:@"二手车"];
    
        return cell;
        } else {
            NormolTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"normolCell"];
            CellModel *model = [self.dataSource objectAtIndex:indexPath.row];
            cell.model = model;
            
            return cell;
        }
    
        
    }
    // cell高度
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        if (indexPath.row == 0) {
            
            return  100;
        } else
            
            return 80;
            
        
    }
    // 分区数
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
      
        return 1;
        
    }
    // 头视图
    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
    
    // ***此处调用封装好的轮播图***
     // 轮播图
     FLLCycleImages *cycleImage = [[FLLCycleImages alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 200)];
     cycleImage.cycleScrollView.showsHorizontalScrollIndicator = YES;
     [self.view addSubview:cycleImage];
     NSArray *array = [NSArray arrayWithArray:_cycleArray];
    //    NSArray *array = @[@"newsPic1", @"newsPic3", @"newsPic4"];
     [cycleImage setImagesWithArray:array];
     return cycleImage;
     ```
    

    }
    // 头视图高度

    • (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
      {

      return 200;
      }

    • (void)didReceiveMemoryWarning {
      [super didReceiveMemoryWarning];
      // Dispose of any resources that can be recreated.
      }
    @end
    // 运行效果如下, 以后涉及到轮播图, 直接引用封装好的文件调用即可.
    ![QQ0.png](https://img.haomeiwen.com/i2435291/6871bbee5c52dab8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

    相关文章

      网友评论

          本文标题:封装轮播图(没加定时器)

          本文链接:https://www.haomeiwen.com/subject/nbexsttx.html