qh_1.gif
先上效果圖荞估。
在TableViewCell中內(nèi)嵌scrollView其實(shí)就是將可以滑動的scrollView用[cell.contentView add:scrollView]添加
直接上代碼:
首先創(chuàng)建一個ScrollImage繼承UIView
在ScrollImage.h中添加接口方法
@interface ScrollImage : UIView
/**
* @frame: 外界傳來的frame 即scrollView上每個UIImageView的大小
*
* @Array: 外界存放UIImage的數(shù)組
*/
- (instancetype)initWithFrame:(CGRect)frame withArray:(NSMutableArray *)array;
@end
在ScrollImage.m中:
#import "ScrollImage.h"
@interface ScrollImage ()<UIScrollViewDelegate> {
CGRect backFrame; // 傳來的frame
CGFloat Margin; // margin為兩邊空隙寬度
}
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSMutableArray *imageArr; // 外界傳來的圖片數(shù)組
@end
@implementation ScrollImage
- (UIScrollView *)scrollView {
if (!_scrollView) {
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, backFrame.size.height)];
_scrollView.contentSize = CGSizeMake(backFrame.size.width * _imageArr.count, 0);
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.bounces = NO;
_scrollView.delegate = self;
[self addSubview:_scrollView];
}
return _scrollView;
}
- (instancetype)initWithFrame:(CGRect)frame withArray:(NSMutableArray *)array {
self = [super initWithFrame:frame];
if (self) {
_imageArr = array;
backFrame = frame;
CGFloat kWidth = frame.size.width * _imageArr.count;
CGRect Frame = self.frame;
Frame.size.width = kWidth;
self.frame = Frame;
// 觸發(fā)懶加載
self.scrollView.backgroundColor = [UIColor clearColor];
[self addImageViewArr];
}
return self;
}
- (void)addImageViewArr {
NSInteger index = 0;
for (UIImage *image in _imageArr) {
UIImageView *imageV = [[UIImageView alloc] initWithImage:image];
imageV.frame = CGRectMake(index * backFrame.size.width, 0, backFrame.size.width, backFrame.size.height);
// 給圖片加手勢
imageV.userInteractionEnabled = YES;
imageV.tag = index;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click:)];
[imageV addGestureRecognizer:tap];
[_scrollView addSubview:imageV];
index++;
}
}
- (void)click:(UITapGestureRecognizer *)sender {
UIImageView *imageVV = (UIImageView *)sender.view;
NSLog(@"第%ld張圖片", imageVV.tag);
}
然后在ViewController.m中調(diào)用
#import "ViewController.h"
#import "ScrollImage.h"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@end
@implementation ViewController
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:(UITableViewStylePlain)];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
}
return _tableView;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 200;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class]) forIndexPath:indexPath];
// 這里用的是系統(tǒng)的cell 如果自定義的話 要在自定義cell導(dǎo)入頭文件ScrollImage 并用cell.contentView 添加scrollView
UIImage *image1 = [UIImage imageNamed:@"12.jpg"];
UIImage *image2 = [UIImage imageNamed:@"19.jpg"];
UIImage *image3 = [UIImage imageNamed:@"25.jpg"];
UIImage *image4 = [UIImage imageNamed:@"29.jpg"];
NSMutableArray *array = @[image1, image2, image3, image4, image1, image2, image3, image4].mutableCopy;
ScrollImage *scrImage = [[ScrollImage alloc] initWithFrame:CGRectMake(0, 10, 100, 180) withArray:array];
[cell.contentView addSubview:scrImage];
return cell;
}
這次的代碼就到這啦榛瓮。