Table View的簡單使用

Table View簡單描述:java

    在iPhone和其餘IOS的不少程序中都會看到Table View的出現,除了通常的表格資料展現以外,設置的屬性資料每每也用到Table View,Table View主要分爲如下兩種:設計模式

  •  Plain:這是普通的列表風格
  •  Grouped :這是分塊風格。

對 於UITableView,我們有一些特殊的概念和術語,好比說咱們成Table View的一行爲Cell,而許多的Cell能夠組成Section,每一個Section上下又分別有Header和Footer,許多個的 Section則組成了整個Table ,固然Table也有Header和Footer,下面看兩種圖就能明白這幾個拗口名詞了:




如今理論知識瞭解的差很少了。今天先作一個Plain樣式的例子,這樣增強對Table view的熟練使用。

一、新建項目

新建一個Single View Application,命名爲TableViewDemo,開發環境是:Xcode 4.3,Iphone 5.1模擬器。
二、Table View放上控件
打開ViewController.xib文件,往ViewController.xib界面上拖拽一個Table View控件到現有的View上,
對齊。

三、鏈接新添加的TableView和ViewController。
選中新添的TableView控件,打開鏈接檢查器(Connection Inspector), 找到delegate和datasource並點中圓圈拉線鏈接到左邊File's Owner圖標上,爲何要把這兩個鏈接File's Owner上呢?這是由於IOS使用的MVC設計模式,View和ViewController之間的對應關係,須要一個橋樑來進行鏈接的(即,對於一個視圖,他如何知道本身的界面的操做應該由誰來響應),這個橋樑就是File's Owner。

四、打開ViewController.h,添加協議和Property (相似與java裏的實現接口)
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>  
  4. @property (strong, nonatomic) NSArray *list;  
  5. @end  

五、打開.m文件,添加:
  1. @synthesize list = _list;  

這是發現有兩個警告,提示未完成的實現,這提示的是UITableViewDelegate, UITableViewDataSource這個兩個頭文件裏的協議的方法未實現。待會咱們去實現它。
六、創建數據
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     // Do any additional setup after loading the view, typically from a nib.  
  5.     NSArray *array = [[NSArray alloc] initWithObjects:@"美國", @"菲律賓",  
  6.                       @"黃巖島", @"中國", @"泰國", @"越南", @"老撾",  
  7.                       @"日本" , nil];   
  8.     self.list = array;   
  9. }  
  10.   
  11. - (void)viewDidUnload  
  12. {  
  13.     [super viewDidUnload];  
  14.     // Release any retained subviews of the main view.  
  15.     self.list = nil;  
  16.       
  17. }  

七、生成row
關鍵的步驟來了,實現tableview添加數據源,返回TableView的行數,返回各行cell實例。
  1. - (UITableViewCell *)tableView:(UITableView *)tableView   
  2.          cellForRowAtIndexPath:(NSIndexPath *)indexPath {   
  3.       
  4.     static NSString *TableSampleIdentifier = @"TableSampleIdentifier";   
  5.       
  6.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:   
  7.                              TableSampleIdentifier];   
  8.     if (cell == nil) {   
  9.         cell = [[UITableViewCell alloc]   
  10.                 initWithStyle:UITableViewCellStyleDefault   
  11.                 reuseIdentifier:TableSampleIdentifier];   
  12.     }   
  13.       
  14.     NSUInteger row = [indexPath row];   
  15.     cell.textLabel.text = [self.list objectAtIndex:row];   
  16.     return cell;   
  17. }  
上面的第二個方法中,
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: TableSampleIdentifier];
這個語句根據標識符TableSampleIdentifier尋找當前能夠重用的UITableViewCell。當某行滑出當前可見區域後,咱們重用它所對應的UITableViewCell對象,那麼就能夠節省內存和資源。
這裏UITableViewCellStyleDefault是表示UITableViewCell風格的常數,除此以外,還有其餘風格,後面將會用到。
注意參數(NSIndexPath *)indexPath,它將行號row和部分號section合併了,經過[indexPath row];獲取行號。以後給cell設置其文本:
cell.textLabel.text = [self.list objectAtIndex: row];

八、如今一個簡單的TableView就弄好看,運行下看效果
、、

九、添加圖片。
在項目上add files到項目,提交兩張小圖片,而後在cell返回以前添加以下代碼
  1. NSUInteger row = [indexPath row];   
  2.     cell.textLabel.text = [self.list objectAtIndex:row];   
  3.     UIImage *image = [UIImage imageNamed:@"qq"];   
  4.     cell.imageView.image = image;   
  5.     UIImage *highLighedImage = [UIImage imageNamed:@"youdao"];   
  6.     cell.imageView.highlightedImage = highLighedImage;  
  7.     return cell;   

效果以下:


十、設置行的風格
表示UITableViewCell風格的常量有:

UITableViewCellStyleDefault
UITableViewCellStyleSubtle
UITableViewCellStyleValue1
UITableViewCellStyleValue2
能夠本身修改看看效果。能夠添加一個detail

 cell.detailTextLabel.text =@"打打打打";atom

return cell; spa





十一、選擇table裏的某一行

在.m文件@end以前編寫  -(void)table  這時會自動提示能夠實現的方法,


咱們選擇這個方法

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath.net

選中是作個提示,提示選中了那個信息,代碼實現以下:設計

  1. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{  
  2.     NSString *rowString = [self.list objectAtIndex:[indexPath row]];  
  3.     UIAlertView * alter = [[UIAlertView alloc] initWithTitle:@"選中的行信息" message:rowString delegate:self cancelButtonTitle:@"肯定" otherButtonTitles:nil, nil];  
  4.     [alter show];  
  5. }  

效果:


以上是Plain風格的TableView

相關文章
相關標籤/搜索