編輯:關於 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
tag 判斷是哪一個在詢問資料,這是共用列表樣板時最省事的做法。_cell 宣告成類別成員變數,實務上應改為區域變數,避免多個列表共用同一份參考而互相覆寫。pagingEnabled = YES,就能得到吸附式的翻頁體驗。以上就是「一個 UIScrollView 內含兩個 UITableView」的完整實作。若頁數更多,也可以改以 UICollectionView 或 UIScrollView 的 pageControl 搭配資料驅動的方式擴充,會比手寫多個 tableView 更好維護。
Android 執行緒完全解析:從單核多工到 Handler、MessageQueue 與 AsyncTask
什麼是執行緒? 執行緒本質上就是一串指令(也就是程式碼),我們把它交給作業系統去執行。 一般來說,CPU 的每一個核心在任一時刻只能處理一條執行緒。多核處理器(目前多數
Android 與設計模式——代理(Proxy)模式
閻宏博士在《JAVA 與模式》一書的開篇,對代理(Proxy)模式下了這樣的定義: 代理模式屬於對象的結構型模式。它為某個對象提供一個代理對象,並由代理對象來控制對原對象
Android 根據螢幕寬度按比例縮放圖片
ImageView 有 scaleType 屬性可以縮放圖片,讓圖片鋪滿螢幕寬度,但往往會出現壓縮或裁剪的情況。 ImageView 的 scaleType 屬性分別是
Android 內部存儲實踐:文件讀寫與 Cache 目錄的使用
Android 的存儲方式有好幾種,內部存儲是最基礎的一種。它的特點是每個應用擁有自己私有的目錄,卸載時相關數據會被系統自動清除,因此適合存放不需要與外部共享的數據。