TableViewCell內(nèi)嵌ScrollView

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;
}

這次的代碼就到這啦榛瓮。

附上Demo https://github.com/catcups/TableViewAddscrollView

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市焕议,隨后出現(xiàn)的幾起案子茄菊,更是在濱河造成了極大的恐慌唱捣,老刑警劉巖舶胀,帶你破解...
    沈念sama閱讀 218,284評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件概说,死亡現(xiàn)場離奇詭異,居然都是意外死亡嚣伐,警方通過查閱死者的電腦和手機(jī)糖赔,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來轩端,“玉大人放典,你說我怎么就攤上這事』穑” “怎么了奋构?”我有些...
    開封第一講書人閱讀 164,614評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長拱层。 經(jīng)常有香客問我弥臼,道長,這世上最難降的妖魔是什么舱呻? 我笑而不...
    開封第一講書人閱讀 58,671評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮悠汽,結(jié)果婚禮上箱吕,老公的妹妹穿的比我還像新娘。我一直安慰自己柿冲,他們只是感情好茬高,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,699評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著假抄,像睡著了一般怎栽。 火紅的嫁衣襯著肌膚如雪丽猬。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,562評論 1 305
  • 那天熏瞄,我揣著相機(jī)與錄音脚祟,去河邊找鬼。 笑死强饮,一個胖子當(dāng)著我的面吹牛由桌,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播邮丰,決...
    沈念sama閱讀 40,309評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼行您,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了剪廉?” 一聲冷哼從身側(cè)響起娃循,我...
    開封第一講書人閱讀 39,223評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎斗蒋,沒想到半個月后捌斧,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,668評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡吹泡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,859評論 3 336
  • 正文 我和宋清朗相戀三年骤星,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片爆哑。...
    茶點(diǎn)故事閱讀 39,981評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡洞难,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出揭朝,到底是詐尸還是另有隱情队贱,我是刑警寧澤,帶...
    沈念sama閱讀 35,705評論 5 347
  • 正文 年R本政府宣布潭袱,位于F島的核電站柱嫌,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏屯换。R本人自食惡果不足惜编丘,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,310評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望彤悔。 院中可真熱鬧嘉抓,春花似錦、人聲如沸晕窑。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,904評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽杨赤。三九已至敞斋,卻和暖如春截汪,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背植捎。 一陣腳步聲響...
    開封第一講書人閱讀 33,023評論 1 270
  • 我被黑心中介騙來泰國打工衙解, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留虹茶,地道東北人患朱。 一個月前我還...
    沈念sama閱讀 48,146評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像彩掐,于是被迫代替她去往敵國和親医咨。 傳聞我的和親對象是個殘疾皇子枫匾,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,933評論 2 355

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