Table View簡單描述:java
在iPhone和其餘IOS的不少程序中都會看到Table View的出現,除了通常的表格資料展現以外,設置的屬性資料每每也用到Table View,Table View主要分爲如下兩種:設計模式
![](http://static.javashuo.com/static/loading.gif)
- 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裏的實現接口)
- #import <UIKit/UIKit.h>
-
- @interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
- @property (strong, nonatomic) NSArray *list;
- @end
五、打開.m文件,添加:
- @synthesize list = _list;
這是發現有兩個警告,提示未完成的實現,這提示的是UITableViewDelegate, UITableViewDataSource這個兩個頭文件裏的協議的方法未實現。待會咱們去實現它。
六、創建數據
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- NSArray *array = [[NSArray alloc] initWithObjects:@"美國", @"菲律賓",
- @"黃巖島", @"中國", @"泰國", @"越南", @"老撾",
- @"日本" , nil];
- self.list = array;
- }
-
- - (void)viewDidUnload
- {
- [super viewDidUnload];
- // Release any retained subviews of the main view.
- self.list = nil;
-
- }
七、生成row
關鍵的步驟來了,實現tableview添加數據源,返回TableView的行數,返回各行cell實例。
- - (UITableViewCell *)tableView:(UITableView *)tableView
- cellForRowAtIndexPath:(NSIndexPath *)indexPath {
-
- static NSString *TableSampleIdentifier = @"TableSampleIdentifier";
-
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
- TableSampleIdentifier];
- if (cell == nil) {
- cell = [[UITableViewCell alloc]
- initWithStyle:UITableViewCellStyleDefault
- reuseIdentifier:TableSampleIdentifier];
- }
-
- NSUInteger row = [indexPath row];
- cell.textLabel.text = [self.list objectAtIndex:row];
- return cell;
- }
上面的第二個方法中,
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就弄好看,運行下看效果
![](http://static.javashuo.com/static/loading.gif)
、、
九、添加圖片。
在項目上add files到項目,提交兩張小圖片,而後在cell返回以前添加以下代碼
- NSUInteger row = [indexPath row];
- cell.textLabel.text = [self.list objectAtIndex:row];
- UIImage *image = [UIImage imageNamed:@"qq"];
- cell.imageView.image = image;
- UIImage *highLighedImage = [UIImage imageNamed:@"youdao"];
- cell.imageView.highlightedImage = highLighedImage;
- 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
選中是作個提示,提示選中了那個信息,代碼實現以下:設計
- -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
- NSString *rowString = [self.list objectAtIndex:[indexPath row]];
- UIAlertView * alter = [[UIAlertView alloc] initWithTitle:@"選中的行信息" message:rowString delegate:self cancelButtonTitle:@"肯定" otherButtonTitles:nil, nil];
- [alter show];
- }
效果:
![](http://static.javashuo.com/static/loading.gif)
以上是Plain風格的TableView