01.輪播圖之二 :tableView 輪播

 在作這個tablevew輪播的時候,重要的就是修改frame 和view 的翻轉了::::數組

 也是不難的,概要的設計和scroll 輪播是一致的;ui

首先是 .h 的文件atom

@interface TableViewShuffling : UIView

@property (nonatomic,strong)NSArray *array;

@end

 

重要的點在.m 文件中加載了詳細的註釋spa

@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 的滾動視圖也基本完成,很久沒寫了,過程有點曲折;設計

總結下重點:::code

  1. frame 翻轉先後設置
  2. row Height 的設置
  3. table 翻轉後 cell正常顯示,必定要翻轉回去

外部調用方法:::::orm

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

 

辛苦的我,今天想早點下班,明天繼續……………………blog

相關文章
相關標籤/搜索