由于看到腾讯新闻的一个广告的效果,滑动tableview,某一个cell的广告会逐渐显示完整.于是从网上看了一下大佬的代码,在此写下一些自己的理解权当做为笔记!
下面直接上代码:
#import "ViewController.h" #import "SHowCell.h" @interface ViewController ()<UITableViewDataSource,UITableViewDelegate> @property (nonatomic,strong)UITableView *tableView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; [self.view addSubview:self.tableView]; } - (UITableView *)tableView{ if (!_tableView) { _tableView = [[UITableView alloc]initWithFrame:self.view.frame style:(UITableViewStylePlain)]; _tableView.dataSource = self; _tableView.delegate = self; } return _tableView; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 11; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 200; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ //第6个Cell显示为图片 if (indexPath.row != 5) { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; } cell.backgroundColor = [UIColor redColor]; cell.textLabel.text = @"这是一条新闻内容"; return cell; } else { SHowCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"]; if (!cell) { cell = [[SHowCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell1"]; } return cell; } } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { //开始显示图片时的偏移量 CGFloat contentPointY = 5 * 200 - [UIScreen mainScreen].bounds.size.height; //scrollView的偏移量 CGFloat contentOffSet = scrollView.contentOffset.y; //图片开始显示和结束显示的偏移量判断 if (contentOffSet >= contentPointY && contentOffSet <= 4 * 200) { NSIndexPath *indexs = [NSIndexPath indexPathForRow:5 inSection:0]; SHowCell *showcell = [self.tableView cellForRowAtIndexPath:indexs]; showcell.contendOffset = contentOffSet - contentPointY; } }
自定义一个图片显示的cell
@interface SHowCell : UITableViewCell
@property (nonatomic,assign) CGFloat contendOffset;
@end
#import "SHowCell.h" @interface SHowCell () @property (nonatomic,strong) UIScrollView *scrollView; @property (nonatomic,strong) UIImageView *myimageView; @end @implementation SHowCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200)]; self.scrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height); self.scrollView.alwaysBounceVertical = YES; [self.contentView addSubview:self.scrollView]; self.scrollView.userInteractionEnabled = NO; self.myimageView = [[UIImageView alloc]initWithFrame:(CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200))]; self.myimageView.image = [UIImage imageNamed:@"这是一张广告图片"]; CGFloat height = self.myimageView.image.size.height; //- height + cellHeight 从图片的底部开始加载 根据图片的高度进行加载 self.myimageView.frame = CGRectMake(0, - height + 200, [UIScreen mainScreen].bounds.size.width, height); [self.scrollView addSubview:self.myimageView]; } return self; } - (void)setContendOffset:(CGFloat)contendOffset{ _contendOffset = contendOffset; [self.scrollView setContentOffset:CGPointMake(0, -_contendOffset) animated:NO]; }
这是一个简单的笔记,如果需要添加的自己项目中,还是需要开动小脑筋,毕竟bug是无情的!!!