01.轮播图之二 :tableView 轮播
生活随笔
收集整理的這篇文章主要介紹了
01.轮播图之二 :tableView 轮播
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?在做這個tablevew輪播的時候,重要的就是修改frame 和view 的翻轉了::::
?也是不難的,概要的設計和scroll 輪播是一致的;
首先是 .h 的文件
@interface TableViewShuffling : UIView@property (nonatomic,strong)NSArray *array;@end?
重要的點在.m 文件中加載了詳細的注釋
@interface TableViewShuffling ()<UITableViewDelegate,UITableViewDataSource>@property(nonatomic,strong)UITableView *tableView;@property(nonatomic,strong)NSMutableArray *tableArray;@end@implementation TableViewShuffling @synthesize array = _array;- (instancetype)initWithFrame:(CGRect)frame{if ( self = [super initWithFrame:frame]) {}return self; }-(UITableView*)tableView{if (_tableView == nil) {
/*
_tableView.transform = CGAffineTransformMakeRotation(-M_PI / 2);
CGRect tabelRect = CGRectMake(10, 10, self.frame.size.height-20, self.frame.size.width-20); 重點:::
因為table view 翻轉 90度角 ,所以frame 設計的時候 寬高 ==互 換===了
*/CGRect tabelRect = CGRectMake(10, 10, self.frame.size.height-20, self.frame.size.width-20);_tableView = [[UITableView alloc] initWithFrame:tabelRect style:UITableViewStylePlain];[self addSubview:self.tableView];_tableView.delegate = self;_tableView.dataSource = self;_tableView.pagingEnabled = YES; // scrollbar 不顯示//tableview逆時針旋轉90度。_tableView.transform = CGAffineTransformMakeRotation(-M_PI / 2); /*
_tableView.center = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2); 重點::: 同樣,因為翻轉的關系,要把table 重寫設置初始位置
*/_tableView.center = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2);}return _tableView; }-(void)setArray:(NSArray *)array{NSAssert(array.count != 0, @"傳入的滾動數組是 空的");_array = array;[self prepareData];[self prepareUI]; }-(void)prepareUI{
/*
跳轉到 row 1===
*/[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];[self.tableView reloadData]; }- (void)prepareData{self.tableArray = [NSMutableArray new];// 首位 添加數組最后的元素 [self.tableArray addObject:_array.lastObject];// 添加數組元素 [self.tableArray addObjectsFromArray:_array];// 末尾 補充第一個元素 [self.tableArray addObject:_array.firstObject]; }-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ /*
row height 千萬分清楚 應該是 width 還是haigh 的值
*/return self.frame.size.width-20; }-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{return self.tableArray.count; }- (UITableViewCell *)tableView :( UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath*)indexPath {/*
為什么 不要用系統cell,必須自定義??
因為你設置 這個tableShuffling view 高度小的時候,會影響titlelabel 等屬性顯示不全,因為翻轉的時候,他們的位置沒有改變
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];if (cell == nil) {cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];// cell順時針旋轉90度cell.contentView.transform = CGAffineTransformMakeRotation(M_PI / 2);}cell.textLabel.text = [NSString stringWithFormat:@"';llkjjjjjjhgfds1234567890-=qwertyuioyuiop[asdfghjkl;zxcvbnm,====%ld",(long)indexPath.row];cell.textLabel.numberOfLines = 0;cell.contentView.backgroundColor = (UIColor*)self.tableArray[indexPath.row];cell.selectionStyle = UITableViewCellSelectionStyleNone;return cell;*/ShufflingCell *cell = [ShufflingCell getCellForTableView:tableView withIdentifier:@"ShufflingCell" andIndexPath:indexPath];
/*
cell.contentView.transform = CGAffineTransformMakeRotation(M_PI / 2); table View 翻轉了,但是要把 cell 翻轉回正常的狀態,否則自定義的cell 顯示也是翻轉的
*/cell.contentView.transform = CGAffineTransformMakeRotation(M_PI / 2);cell.contentView.backgroundColor = (UIColor*)self.tableArray[indexPath.row];cell.selectionStyle = UITableViewCellSelectionStyleNone;return cell;}-(void)scrollViewDidScroll:(UIScrollView *)scrollView{if (scrollView == self.tableView) {//檢測移動的位移if (scrollView.contentOffset.y == (self.tableArray.count-1)*(self.frame.size.width-20) ) {[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];}else if (scrollView.contentOffset.y == 0){[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:(self.tableArray.count-2) inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];}else{// 正常滾動 }} }
?
table view 的滾動視圖也基本完成,好久沒寫了,過程有點曲折;
總結下重點:::
外部調用方法:::::
-(void)prepareTableShuffling{TableViewShuffling *tableffling = [[TableViewShuffling alloc]initWithFrame:CGRectMake(10, 150, self.view.frame.size.width -20, 150)];[self.view addSubview:tableffling];;tableffling.array = self.arr; }?
辛苦的我,今天想早點下班,明天繼續……………………
轉載于:https://www.cnblogs.com/Bob-blogs/p/6769875.html
總結
以上是生活随笔為你收集整理的01.轮播图之二 :tableView 轮播的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 蓝牙低耗能(LBE)技术
- 下一篇: 日志文件清理代码