鍍金池/ 教程/ iOS/ iOS - Table View(表格視圖)
iOS - Switches(切換/開關(guān))
iOS - Labels(標(biāo)簽)
iOS - Table View(表格視圖)
IOS - 攝像頭管理
iOS - Text View(文本視圖)
IOS - 開發(fā)環(huán)境配置
iOS教程
iOS - Twitter & Facebook
iOS - UI元素
iOS - iAd 整合
IOS - 應(yīng)用程序調(diào)試
iOS - Split View(分割視圖)
iOS - Status Bar(狀態(tài)欄)
iOS - Navigation Bar(導(dǎo)航欄)
iOS - Tab bar(標(biāo)簽欄)
IOS - 文件處理
IOS - 自動(dòng)布局
iOS - Image View(圖像視圖)
iOS - 應(yīng)用程序內(nèi)購買
iOS - Pickers(選取器)
iOS - Delegates實(shí)例
iOS - 創(chuàng)建第一個(gè)iPhone應(yīng)用
iOS (iPhone, iPad)教程
iOS - 發(fā)送電子郵箱(Email)
iOS - View Transitions(視圖轉(zhuǎn)換)
iOS - 內(nèi)存管理
iOS - Icons(圖標(biāo))
iOS - 音頻和視頻
iOS - Storyboards(演示圖板演)
iOS - Buttons(按鈕)
iOS - Text Field(文本域)
iOS - Sliders(滑動(dòng)條)
iOS - Scroll View(滾動(dòng)視圖)
IOS - 輸入類型 文本字段
iOS - 位置處理
iOS - Accelerometer(加速度傳感器)
IOS - 快速入門
iOS - SQLite 數(shù)據(jù)庫
iOS - GameKit
IOS - 訪問地圖
iOS - Objective-C基礎(chǔ)
iOS - Toolbar(工具欄)
IOS - 動(dòng)作和插座(Outlets)
iOS - Alerts(警示)
IOS - 通用應(yīng)用程序

iOS - Table View(表格視圖)

使用表格視圖

它是用于顯示一個(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 如下所示。

iOS Tutorial

2. 設(shè)置委托和數(shù)據(jù)源文件所有者tableview,通過右擊并選擇數(shù)據(jù)源和委托。設(shè)置數(shù)據(jù)源,如下所示。

iOS Tutorial

3. 然后為 tableview 創(chuàng)建一個(gè)IBOutlet 命名為 myTableView。這是顯示在下面的圖像。

iOS TutorialiOS Tutorial

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];

    }