iOS開發UI篇—直接使用UITableView Controller

本文轉載自:https://www.cnblogs.com/wendingding/p/3764754.html 作者:wendingding 轉載請註明該聲明。

iOS開發UI篇—直接使用UITableView Controller

一、一般過程

 1 //
2 // YYViewController.h
3 // UITableView Controller
4 //
5 // Created by 孔醫己 on 14-6-2.
6 // Copyright (c) 2014年 itcast. All rights reserved.
7 //
8
9 #import <UIKit/UIKit.h>
10
11 @interface YYViewController : UIViewController
12
13 @end

系統storyboard中默認的控制器爲:ViewController

這樣的話如果整個程序界面都只是使用UITableView來搭建,那麼一般需要完成以下相對繁瑣的步驟:

(1)向界面上拖一個UItableview

(2)設置數據源

(3)設置代理

(4)遵守代理協議

 上述過程相對繁瑣,且還需要手動的設置數據源,代理,遵守協議等,容易遺漏,下面推薦直接使用UITableView Controller。
 
二、使用UITableView Controller
  爲了簡化操作,推出下面的方法。
  即如果在界面上僅僅只是需要用來展示一個UITableView,那麼可以讓主控制器直接繼承於UITableView Controller
 1 //
2 // YYViewController.h
3 // UITableView Controller
4 //
5 // Created by 孔醫己 on 14-6-2.
6 // Copyright (c) 2014年 itcast. All rights reserved.
7 //
8
9 #import <UIKit/UIKit.h>
10
11 @interface YYViewController : UITableViewController
12
13 @end
 
 直接讓控制器繼承UITableView controller,然後在storyboard中把以前的界面刪掉,拖一個tableview controller就可以了。
 
注意:需要和主控制器類進行關聯。
UITableView Controller裏面有個tableview屬性,在控制器中通過self.view獲取出來的視圖就是一個tableview。
即self.view=self.taleview。
且它默認已經把他的協議和數據源都已經實現好了,不再需要進行連線。
 1 //  UITableViewController.h
2 // UIKit
3 //
4 // Copyright (c) 2008-2013, Apple Inc. All rights reserved.
5 //
6 #import <Foundation/Foundation.h>
7 #import <UIKit/UIViewController.h>
8 #import <UIKit/UITableView.h>
9 #import <UIKit/UIKitDefines.h>
10
11 // Creates a table view with the correct dimensions and autoresizing, setting the datasource and delegate to self.
12 // In -viewWillAppear:, it reloads the table's data if it's empty. Otherwise, it deselects all rows (with or without animation) if clearsSelectionOnViewWillAppear is YES.
13 // In -viewDidAppear:, it flashes the table's scroll indicators.
14 // Implements -setEditing:animated: to toggle the editing state of the table.
15
16 NS_CLASS_AVAILABLE_IOS(2_0) @interface UITableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
17
18 - (id)initWithStyle:(UITableViewStyle)style;
19
20 @property(nonatomic,retain) UITableView *tableView;
21 @property(nonatomic) BOOL clearsSelectionOnViewWillAppear NS_AVAILABLE_IOS(3_2); // defaults to YES. If YES, any selection is cleared in viewWillAppear:
22
23 @property (nonatomic,retain) UIRefreshControl *refreshControl NS_AVAILABLE_IOS(6_0);
24
25 @end
點擊右鍵,可以發現數據源和代理都已經連好了。
 
(應該把繼承自uiviewcontroller的控制器幹掉,重新拖一個tableview controller,和主控制器進行連線。)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章