iOS視圖學習——TableView

本文轉載於容芳志的blog http://blog.csdn.net/totogo2010/article/details/7642908,在此表示感謝。

Table View簡單描述:

    在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的熟練使用。

1、新建項目

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

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

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

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

這是發現有兩個警告,提示未完成的實現,這提示的是UITableViewDelegate, UITableViewDataSource這個兩個頭文件裏的協議的方法未實現。待會我們去實現它。
6、建立數據
  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. }  

7、生成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];

8、現在一個簡單的TableView就弄好看,運行下看效果
、、

9、添加圖片。
在項目上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;   

效果如下:


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

UITableViewCellStyleDefault
UITableViewCellStyleSubtle
UITableViewCellStyleValue1
UITableViewCellStyleValue2
可以自己修改看看效果。可以添加一個detail

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

return cell; 





11、選擇table裏的某一行

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


我們選擇這個方法

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

選中是做個提示,提示選中了那個信息,代碼實現如下:

  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


著作權聲明:本文由http://blog.csdn.net/totogo2010/原創,歡迎轉載分享。請尊重作者勞動,轉載時保留該聲明和作者博客鏈接,謝謝!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章