Bear好陣子沒來更新了...由於最近工作繁忙一直沒更新(囧)
上次入門篇教學我們談到什麼是協定跟委派 (Protocol & Delegate)
這次我們繼續來介紹iOS內另一個重量級元件: UITableView,
也就是各位在App內隨處可見的表格呈現!
上次入門篇教學我們談到什麼是協定跟委派 (Protocol & Delegate)
這次我們繼續來介紹iOS內另一個重量級元件: UITableView,
也就是各位在App內隨處可見的表格呈現!
相信經過前面那篇Protocol與Delegate的教學後
大家腦袋應該都打結了吧!
讓我們實際用一下UITableView來感受委派與協定的用法
首先我們要在@interface ViewController ()後面加上<UITableViewDataSource,UITableViewDelegate>
表示本類別遵循UITableView的兩個委派協定
之後我們就要在本類別添加規定的方法
忘記的話xcode也會很貼心的提醒喔!
然後在viewDidLoad內建立UITableView
UITableView *takoTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
大小範圍設定跟畫面一樣大self.view.bounds
款式有Grouped跟plain可以選擇
takoTableView.dataSource = self;
takoTableView.delegate = self;
委派都是self(viewController)
[self.view addSubview: takoTableView];
最重要的還是要貼在畫面上!千萬別忘了
大家腦袋應該都打結了吧!
讓我們實際用一下UITableView來感受委派與協定的用法
首先我們要在@interface ViewController ()後面加上<UITableViewDataSource,UITableViewDelegate>
表示本類別遵循UITableView的兩個委派協定
之後我們就要在本類別添加規定的方法
忘記的話xcode也會很貼心的提醒喔!
然後在viewDidLoad內建立UITableView
UITableView *takoTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
大小範圍設定跟畫面一樣大self.view.bounds
款式有Grouped跟plain可以選擇
takoTableView.dataSource = self;
takoTableView.delegate = self;
委派都是self(viewController)
[self.view addSubview: takoTableView];
最重要的還是要貼在畫面上!千萬別忘了
協定中@required 是規定要在本類別使用的方法
@optional 則是自由調用
下面添加三個方法
其中兩個是遵循協定一定要有的
三個方法,我們的邏輯分別是
1.設定section為2個
2.第一個section有三列,第二個section有五列
3.設定列的內容
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0) {
return 3;
}
else if (section == 1){
return 5;
}
return 0;
//if 之外也要設一個值讓return回傳,不然是無法編譯的
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
static NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
// 以上兩行是制式寫法
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
// cell就是我們表格的內容了
return cell;
}
其中兩個是遵循協定一定要有的
三個方法,我們的邏輯分別是
1.設定section為2個
2.第一個section有三列,第二個section有五列
3.設定列的內容
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0) {
return 3;
}
else if (section == 1){
return 5;
}
return 0;
//if 之外也要設一個值讓return回傳,不然是無法編譯的
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
static NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
// 以上兩行是制式寫法
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
// cell就是我們表格的內容了
return cell;
}
另外眼尖的讀者有沒有發現箭頭指的地方有一行
#pragma mark - TableView DataSource
#pragma mark 就是書簽的功能喔
如下圖箭頭處,點擊後會列出我們所使用的方法
標簽則可讓我們更好的分類, 當code變得相當巨大的時候就很好用囉!
#pragma mark - TableView DataSource
#pragma mark 就是書簽的功能喔
如下圖箭頭處,點擊後會列出我們所使用的方法
標簽則可讓我們更好的分類, 當code變得相當巨大的時候就很好用囉!
這次的教學雖然簡單, 但是已經把前面的原理給完整的結合囉!
剛入門的朋友記得要多加練習喔!
借由UITableView後有沒有對委派協定更理解呢?
一般來說,UITableView通常都是搭配NSArray 或NSDictionary等集合物件使用
等下次有空Bear再介紹囉!
這是很有趣的挑戰,卡關的話歡迎隨時寄信問Bear喔!!
[入門系列]
1. Xcode 5 - 你的第一支Hello World iOS程式
2. Xcode 5 - UIView 與 UILabel
3. Xcode 5 - 第一顆互動按鈕 UIButton
4. Xcode 5 - 協定與委派
[中級系列]
1. OpenSource - 可拖曳的UICollectionView (DraggableCollectionView)
[進階系列]
1. iOS 製作廣播電台
2. iOS 製作視訊串流
3. iOS 與Arduino製作藍芽聊天室
4. iOS 製作長曝光相機
5. iOS CoreblueTooth FrameWork
喜歡這篇文章嗎? 加入Takobear粉絲團吧!
剛入門的朋友記得要多加練習喔!
借由UITableView後有沒有對委派協定更理解呢?
一般來說,UITableView通常都是搭配NSArray 或NSDictionary等集合物件使用
等下次有空Bear再介紹囉!
這是很有趣的挑戰,卡關的話歡迎隨時寄信問Bear喔!!
[入門系列]
1. Xcode 5 - 你的第一支Hello World iOS程式
2. Xcode 5 - UIView 與 UILabel
3. Xcode 5 - 第一顆互動按鈕 UIButton
4. Xcode 5 - 協定與委派
[中級系列]
1. OpenSource - 可拖曳的UICollectionView (DraggableCollectionView)
[進階系列]
1. iOS 製作廣播電台
2. iOS 製作視訊串流
3. iOS 與Arduino製作藍芽聊天室
4. iOS 製作長曝光相機
5. iOS CoreblueTooth FrameWork
喜歡這篇文章嗎? 加入Takobear粉絲團吧!