它是用于顯示一個(gè)垂直滾動(dòng)的單元格數(shù)(通常為可重復(fù)使用的單元格)組成的視圖。它具有特殊的功能,如頁眉,頁腳,行和段。
重要的屬性
delegate
dataSource
rowHeight
sectionFooterHeight
sectionHeaderHeight
separatorColor
tableHeaderView
tableFooterView
重要的方法
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath
- (void)reloadData
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
- (NSArray *)visibleCells
示例代碼和步驟
1. 讓加一個(gè) tableview 在 ViewController.xib 如下所示。
2. 設(shè)置委托和數(shù)據(jù)源文件所有者tableview,通過右擊并選擇數(shù)據(jù)源和委托。設(shè)置數(shù)據(jù)源,如下所示。
3. 然后為 tableview 創(chuàng)建一個(gè)IBOutlet 命名為 myTableView。這是顯示在下面的圖像。
4. 然后添加一個(gè)NSMutableArray持有表視圖中顯示的數(shù)據(jù)。
5. ViewController 應(yīng)采用 UITableViewDataSource和UITableViewDelegateprotocols。該ViewController.h 看起來應(yīng)該如下圖所示。
#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate> { IBOutlet UITableView *myTableView; NSMutableArray *myData; } @end
6. 我們應(yīng)該實(shí)現(xiàn)所需的 tableview 委托和數(shù)據(jù)源的方法。更新后 ViewController.m 如下
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // table view data is being set here myData = [[NSMutableArray alloc]initWithObjects: @"Data 1 in array",@"Data 2 in array",@"Data 3 in array", @"Data 4 in array",@"Data 5 in array",@"Data 5 in array", @"Data 6 in array",@"Data 7 in array",@"Data 8 in array", @"Data 9 in array", nil]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Table View Data source - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section{ return [myData count]/2; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath{ static NSString *cellIdentifier = @"cellID"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc]initWithStyle: UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } NSString *stringForCell; if (indexPath.section == 0) { stringForCell= [myData objectAtIndex:indexPath.row]; }上一篇:IOS - 輸入類型 文本字段下一篇:IOS - 快速入門