橫向表格
// // 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