Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

UIScrollView 裡放兩個 UITableView:左右分頁滑動的完整實作

編輯:關於 Android 程式設計

一、效果預覽

本文示範的是在一個 UIScrollView 中並置兩個 UITableView,並透過分頁(paging)效果實現左右切換的經典版型。

二、專案結構

三、程式碼

RootViewController.h

 #import <UIKit/UIKit.h>

@interface RootViewController : UIViewController
<UIScrollViewDelegate, UITableViewDelegate, UITableViewDataSource> {
    UIScrollView *_scrolView;
    UITableView *_tableView;
    UITableView *_tableView2;
    UITableViewCell *_cell;
}
@end 

RootViewController.m

 #import "RootViewController.h"

@interface RootViewController ()
@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initBackGroundView];
}

#pragma mark - functions

-(void)initBackGroundView {
    // 承載兩個 tableView 的外層捲動視圖
    _scrolView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 19, 320, 460)];
    _scrolView.contentSize = CGSizeMake(320 * 2, 460);  // 內容寬度為兩頁
    _scrolView.delegate = self;
    _scrolView.pagingEnabled = YES;                     // 分頁對齊
    _scrolView.showsVerticalScrollIndicator = NO;
    _scrolView.bounces = NO;
    [self.view addSubview:_scrolView];

    // 第一個 tableView
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 19, 320, 460)];
    _tableView.tag = 1;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.scrollEnabled = NO;   // 交由外層 scrollView 統一處理滾動
    [_scrolView addSubview:_tableView];

    // 第二個 tableView,位置在右邊一頁
    _tableView2 = [[UITableView alloc] initWithFrame:CGRectMake(320, 19, 320, 460)];
    _tableView2.tag = 2;
    _tableView2.delegate = self;
    _tableView2.dataSource = self;
    _tableView2.scrollEnabled = NO;
    [_scrolView addSubview:_tableView2];
}

#pragma mark - UITableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 125;
}

-(UITableViewCell *)tableView:(UITableView *)tableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    _cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
    if (_cell == nil) {
        _cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:@"ID"];
    }
    _cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (tableView.tag == 1) {
        _cell.textLabel.text = @"1";
    } else if (tableView.tag == 2) {
        _cell.textLabel.text = @"2";
    }
    return _cell;
}

@end 

幾個關鍵設計說明

  • 為什麼要把 tableView 的 scrollEnabled 關掉?因為內外兩層都能捲動時,手勢會互相搶。把 tableView 設為不可捲動,讓外層 UIScrollView 全权接管,才能做出乾淨的左右分頁切換。
  • 用 tag 區分資料來源。兩個 tableView 共用同一組 delegate 與 dataSource,因此靠 tag 判斷是哪一個在詢問資料,這是共用列表樣板時最省事的做法。
  • 用 dequeue 而非直接配置。另外提醒,範例把 _cell 宣告成類別成員變數,實務上應改為區域變數,避免多個列表共用同一份參考而互相覆寫。
  • contentSize 決定頁數。寬度設為單頁寬的兩倍,配合 pagingEnabled = YES,就能得到吸附式的翻頁體驗。

以上就是「一個 UIScrollView 內含兩個 UITableView」的完整實作。若頁數更多,也可以改以 UICollectionView 或 UIScrollView 的 pageControl 搭配資料驅動的方式擴充,會比手寫多個 tableView 更好維護。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved