### UITableView 與其他相關組件一樣,視圖控制器須實現:數據源(UITableViewDataSource) ,委托(UITableViewDelegate)兩個協議,(然后實現其方法) 。這樣當視圖控制器成為UITableView的數據源與委托時,就能為其工作(調用被實現的方法)。
### 一:創建UITableview,默認是無格式表。

### 1)接下來就是創建輸出口(使視圖控制器得到界面上組建的實例,這里就是UITableView),連接委托(delegate),數據源(datasource),關于他們的具體解釋上一篇日志已經提到,這里不做過多介紹。

### 2)創建一個plist文件用于定義數據(為視圖顯示提供數據):點擊new file----->resource----->property list

### 3)在plist文件中定義數據:

### 4)添加數據規則:

### 5)查看數據代碼:

### 6)由此可見xcode的功能還是很好的,只要我們在界面上直觀的點擊加號,減號,添加數據和數據類型,在后臺就會自動生成代碼格式,很方便。??? ok前期的數據工作就準備好了!
### 二:控制器.h文件遵循UITableViewDataSource,UITableViewDelegate兩個協議,然后.m文件實現其方法(為視圖顯示做工作)。
### 樣例代碼:
### 1)MyTableViewGroupController.h文件中:

### 2)
### 1. MyTableViewGroupController.m文件中:在viewDidLoad方法中初始化數據(從plist文件中獲得數據)

### 2. 實現 協議UITableViewDataSource的五個方法使數據按照操作顯示在屏幕上,這里并沒有實現協議UITableViewDelegate的方法。
~~~
#pragma mark-
#pragma mark Table View Group
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//返回(向系統發送)分區個數,在這里有多少鍵就會有多少分區。
return [self.MyKey count];
}
//所在分區所占的行數。
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//獲取當前分區所對應的鍵(key)。在這里鍵就是分區的標示。
NSString *key=[self.MyKey objectAtIndex:section];
//獲取鍵所對應的值(數組)。
NSArray *nameSec=[self.resource objectForKey:key];
//返回所在分區所占多少行。
return [nameSec count];
}
//向屏幕顯示。
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//獲得所在分區的行數
NSInteger row=[indexPath row];
//獲得分區值
NSInteger section=[indexPath section];
//利用分區獲得鍵值
NSString *key=[self.MyKey objectAtIndex:section];
//利用鍵值獲得其所對應的值
NSArray *MySectionArr=[self.resource objectForKey:key];
//定義標記,用于標記單元格
static NSString *SectionTableMyTag=@"dong";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:SectionTableMyTag];
//如果當前cell沒被實例(程序一開始就會運行下面的循環,直到屏幕上所顯示的單元格格全被實例化了為止,沒有顯示在屏幕上的單元格將會根據定義好的標記去尋找可以重用的空間來存放自己的值)
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SectionTableMyTag];
}
cell.textLabel.text=[MySectionArr objectAtIndex:row];
return cell;
}
//把每個分區打上標記key
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
NSString *key=[self.MyKey objectAtIndex:section];
return key;
}
//在單元格最右放添加索引
-(NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView{
return self.MyKey;
}
~~~
### 3)看運行結果:

### 很明顯這是一個分區索引表。如果只想要無格式表,就是不用寫numberOfSectionsInTableView返回分區數,這個方法。同理如果想要除去索引,只需不寫其方法sectionIndexTitlesForTableView即可。
### 4)直接改為分組表


### 可見既有分組又有索引,但蘋果不建議這樣寫,可能是出于對程序優化的考慮。
- 前言
- (1) iphone開發,自定義Window-based Application 模板及委托運行機制
- (2) iphone 開發 表視圖UITableView結構深層剖析
- (3) iphone 開發 從應用程序看UITableView的:分組,分區,索引,工作原理及其變換法則,plist文件數據定義規則
- (4) iphone 開發 自定義UITableViewCell的子類 ,輕松添加圖片文本信息等
- (5) iphone 開發 在表視圖(UITableView) 中利用UISearchBar實現數據的搜索,視圖的多功能化
- (6) iphone 開發 真正理解委托(delegate)與數據源(data source)
- (7)---01 iphone 開發 數據傳遞 NSNotification 通知機制演示
- (7)---02 iphone 開發 數據傳遞 : 頁面切換與數據的反向傳遞以及協議(protocol)作用的體現
- (8)---01 iphone 開發 大話分析導航欄NavigationController
- (9) iphone 開發 AppSettings , 系統setting與應用程序setting間的數據控制
- (10) iphone 開發 用戶點擊,觸摸和手勢識別 解析