横向表格
// // HYBHorizontalTableView.h // PersonalShoppingMall // // Created by ljy-335 on 14-8-28. // Copyright (c) 2014年 uni2uni. All rights reserved. // #import <UIKit/UIKit.h> @class HYBHorizontalTableView; /*! * @brief 表格数据源代理 */ @protocol HYBHorizontalTableViewDataSource <NSObject> @required /*! * @brief 指定行高 * @param tableView 当前表格对象 * @param index 指定行号 * @return 返回指定的行号的行宽 */ - (CGFloat)horizontalTableView:(HYBHorizontalTableView *)tableView widthForIndex:(NSInteger)index; /*! * @brief 指定行数 * @param tableView 当前表格对象 * @return 返回行数 */ - (NSInteger)numberOfIndexForTableView:(HYBHorizontalTableView *)tableView; /*! * @brief 设置行显示的内容 * @param tableView 当前表格对象 * @param contentView 是在-horizontalTableView:targetRect:forIndex:这里设置的UI视图,这一步需要 * 更新控件显示的内容,比如,设置的是imageView,则这里可以修改imageView.image属性 * @param index 指定行号 */ - (void)horizontalTableView:(HYBHorizontalTableView *)tableView setContentView:(UIView *)contentView ForIndex:(NSInteger)index; /*! * @brief 指定行内容的rect * @param tableView 当前表格对象 * @param targetRect index行下cell要显示的视图的rect * @param index 指定行号 * @return index行下的视图UI布局 */ - (UIView *)horizontalTableView:(HYBHorizontalTableView *)tableView targetRect:(CGRect)targetRect forIndex:(NSInteger)index; @end /*! * @brief 表格响应代理 */ @protocol HYBHorizontalTableViewDelegate <NSObject> @optional /*! * @brief 点击行代理方法 * @param tableView 当前点击的表格对象 * @param index 点击的行号 */ - (void)horizontalTableView:(HYBHorizontalTableView *)tableView selectIndex:(NSInteger)index; /*! * @brief 滚动到行的代理方法 * @param tableView 当前表格对象 * @param index 滚动到的指定的行号 */ - (void)horizontalTableView:(HYBHorizontalTableView *)tableView scrollToIndex:(NSInteger)index; @end /*! * @brief 横向表格功能,相比使用scrollview可以优化内存 * @author huangyibiao */ @interface HYBHorizontalTableView : UIView @property (nonatomic, retain) UITableView *tableView; @property (nonatomic, assign) id<HYBHorizontalTableViewDataSource> dataSource; @property (nonatomic, assign) id<HYBHorizontalTableViewDelegate> delegate; @property (nonatomic, assign) NSInteger currentIndex; @property (nonatomic, assign) BOOL pagingEnabled; /*! * @brief 调用此方法来刷新数据 */ - (void)reloadData; /*! * @brief 滚动到指定的行 * @param index 行号 * @param animation 是否显示动画 */ - (void)horizontalTableViewScrollToIndex:(NSInteger)index animation:(BOOL)animation; /*! * @brief 获取index对应的视图 * @param index 行号 * @return 返回index行对应的视图 */ - (UIView *)viewInHorizontalTableViewWithIndex:(NSInteger)index; @end
// // HYBHorizontalTableView.m // PersonalShoppingMall // // Created by ljy-335 on 14-8-28. // Copyright (c) 2014年 uni2uni. All rights reserved. // #import "HYBHorizontalTableView.h" #import <QuartzCore/QuartzCore.h> #define kTableViewTag 219911 #define kTableViewContentViewTag 123111 @interface HYBHorizontalTableView () <UITableViewDataSource, UITableViewDelegate> { CGSize _size; } @end @implementation HYBHorizontalTableView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { _size = frame.size; self.currentIndex = -1; [self configureLayout]; } return self; } - (void)configureLayout { UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, _size.width, _size.height)]; tableView.tag = kTableViewTag; tableView.delegate = self; tableView.dataSource = self; tableView.separatorStyle = UITableViewCellSeparatorStyleNone; tableView.backgroundColor = [UIColor clearColor]; tableView.transform = CGAffineTransformMakeRotation(-M_PI/2); self.tableView.frame = CGRectMake(0, 0, _size.width, _size.height); tableView.showsVerticalScrollIndicator = NO; tableView.showsVerticalScrollIndicator = NO; [self addSubview:tableView]; return; } #pragma mark - Public Methods - (void)reloadData { _size = self.frame.size; self.tableView.frame = CGRectMake(0, 0, _size.width, _size.height); [self.tableView reloadData]; return; } - (void)setPagingEnabled:(BOOL)pagingEnabled { if (_pagingEnabled != pagingEnabled) { _pagingEnabled = pagingEnabled; self.tableView.pagingEnabled = _pagingEnabled; } return; } - (UIView *)viewInHorizontalTableViewWithIndex:(NSInteger)index { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0]; UITableViewCell *cell = [self.tableView.dataSource tableView:self.tableView cellForRowAtIndexPath:indexPath]; UIView *contentView = [cell viewWithTag:kTableViewContentViewTag]; if (contentView) { return contentView; } return nil; } - (UITableView *)tableView { return (UITableView *)[self viewWithTag:kTableViewTag]; } - (void)horizontalTableViewScrollToIndex:(NSInteger)index animation:(BOOL)animation { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0]; [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:animation]; return; } #pragma mark - UITableViewDataSource - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if ([self.dataSource respondsToSelector:@selector(horizontalTableView:widthForIndex:)]) { return [self.dataSource horizontalTableView:self widthForIndex:indexPath.row]; } return 0.f; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if ([self.dataSource respondsToSelector:@selector(numberOfIndexForTableView:)]) { return [self.dataSource numberOfIndexForTableView:self]; } return 0; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *cellIdentifier = @"HYBHorizontalTableViewCellIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; CGFloat width = [self.dataSource horizontalTableView:self widthForIndex:indexPath.row]; cell.frame = CGRectMake(0, 0, width, _size.height); cell.contentView.frame = cell.bounds; cell.selectionStyle = UITableViewCellSelectionStyleNone; UIView *contentView = [self.dataSource horizontalTableView:self targetRect:cell.contentView.frame forIndex:indexPath.row]; if (!contentView) { contentView = [[UIView alloc] initWithFrame:cell.contentView.bounds]; } contentView.transform = CGAffineTransformMakeRotation(M_PI / 2); contentView.frame = CGRectMake(0, 0, cell.contentView.frame.size.height, cell.contentView.frame.size.width); contentView.tag = kTableViewContentViewTag; [cell.contentView addSubview:contentView]; } CGFloat width = [self.dataSource horizontalTableView:self widthForIndex:indexPath.row]; cell.frame = CGRectMake(0, 0, width, _size.height); cell.contentView.frame = cell.bounds; UIView *contentView = [cell.contentView viewWithTag:kTableViewContentViewTag]; contentView.frame = CGRectMake(0, 0, cell.contentView.frame.size.height, cell.contentView.frame.size.width); [self.dataSource horizontalTableView:self setContentView:contentView ForIndex:indexPath.row]; return cell; } #pragma mark - UITableViewDelegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if ([self.delegate respondsToSelector:@selector(horizontalTableView:selectIndex:)]) { [self.delegate horizontalTableView:self selectIndex:indexPath.row]; } return; } #pragma mark - UIScrollViewDelegate - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (scrollView.pagingEnabled) { CGFloat pageWidth = scrollView.frame.size.width; int currentPage = floor((scrollView.contentOffset.y - pageWidth / 2) / pageWidth) + 1; if (self.currentIndex != currentPage) { if ([self.delegate respondsToSelector:@selector(horizontalTableView:scrollToIndex:)]) { [self.delegate horizontalTableView:self scrollToIndex:currentPage]; } self.currentIndex = currentPage; } } return; } @end
最后更新:2017-04-03 05:40:07