美文网首页
iOS 图片缩放 + 自定义cell

iOS 图片缩放 + 自定义cell

作者: YQ_苍穹 | 来源:发表于2018-04-16 09:17 被阅读0次

    #import "MyTableViewCell.h"

    @implementation MyTableViewCell

    //图片

    - (UIImageView *)headerImg{

        if (!_headerImg) {

            _headerImg = [[UIImageView alloc]initWithFrame:CGRectMake(10, 20, 44, 44)];

            _headerImg.layer.cornerRadius = 44 / 2;

            _headerImg.layer.masksToBounds = YES;

        }

        return _headerImg;

    }

    //标题

    - (UILabel *)titleLabel{

        if (!_titleLabel) {

            _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(70, 5, 80, 30)];

            _titleLabel.font = [UIFont systemFontOfSize:20];

        }

        return _titleLabel;

    }

    //副标题

    - (UILabel *)subLabel{

        if (!_subLabel) {

            _subLabel = [[UILabel alloc]initWithFrame:CGRectMake(70, 40, 300, 30)];

            _subLabel.textColor = [UIColor lightGrayColor];

            _subLabel.font = [UIFont systemFontOfSize:16];

        }

        return _subLabel;

    }

    //时间

    - (UILabel *)timeLabel{

        if (!_timeLabel) {

            _timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(320, 25, 80, 30)];

            _timeLabel.textColor = [UIColor lightGrayColor];

            _timeLabel.font = [UIFont systemFontOfSize:18.];

        }

        return _timeLabel;

    }

    //重写初始化

    - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

    {

        if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

            [self addSubview:self.headerImg];

            [self addSubview:self.titleLabel];

            [self addSubview:self.subLabel];

            [self addSubview:self.timeLabel];

        }

        return self;

    }

    @end

    #import "ViewController.h"#import "MyTableViewCell.h"@interface ViewController (){

        NSArray *array;

        NSDictionary *Dic;

        UITableView *table;

        UIView *headerView;

        UIImageView *image;

    }

    @end

    @implementation ViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 200)];

        UIImage *img = [UIImage imageNamed:@"hearder"];

        image = [[UIImageView alloc]initWithFrame:CGRectMake(([[UIScreen mainScreen] bounds].size.width / 2) - 40 , 0, self.view.frame.size.width, 100)];

        image.image = img;

        [headerView addSubview:image];

        table=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];

        table.showsVerticalScrollIndicator = NO;

        table.rowHeight=80;

        table.delegate=self;

        table.dataSource=self;

        table.tableHeaderView = headerView;

        [self.view addSubview:table];

        NSString *path=[[NSBundle mainBundle]pathForResource:@"cell.plist" ofType:nil];

        //获取所有数据源

        Dic=[NSDictionary dictionaryWithContentsOfFile:path];

        //获取所有的key

        array=[Dic allKeys];

    }

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView{

        CGFloat Yset = table.contentOffset.y;

        //向上偏移量变正  向下偏移量变负

        if (Yset < -64) {

            CGFloat factor = ABS(Yset)+200-64;

            CGRect s = CGRectMake(- ([[UIScreen mainScreen] bounds].size.width*factor / 200 -[[UIScreen mainScreen] bounds].size.width) / 2,- ABS(Yset) + 64, [[UIScreen mainScreen] bounds].size.width * factor / 200, factor);

            image.frame = s;

        }else {

            CGRect hvF = headerView.frame;

            hvF.origin.y = 0;

            headerView.frame = hvF;

            image.frame = CGRectMake(0, hvF.origin.y, [[UIScreen mainScreen] bounds].size.width, 200);

        }

    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

        return array.count;

    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

        static NSString *ID=@"cell";

        MyTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];

        if (!cell) {

            cell=[[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];

        }

        //获得该商品对用的数组信息

        NSArray *dataArr=[Dic objectForKey:[array objectAtIndex:indexPath.row]];

        cell.headerImg.image=[UIImage imageNamed:[dataArr objectAtIndex:0]];

        cell.titleLabel.text=[array objectAtIndex:indexPath.row];

        cell.subLabel.text=dataArr[1];

        cell.timeLabel.text=dataArr[2];

        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;

        return cell;

    }

    @end

    相关文章

      网友评论

          本文标题:iOS 图片缩放 + 自定义cell

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