IOS表沒有限制行數,行數僅受可用儲存空間的限制,表只有一列。是UITableView類的一個執行個體。
表視圖不負責儲存表中的資料。表視圖從遵循UITableViewDelegate協議的對象擷取配置資料,從遵循UITableViewDataSource協議的對象擷取行資料。表中每一行都由一個UITableViewCell表示。
在.h檔案中遵循UITableViewDelegate協議和UITableViewDataSource協議,聲明表視圖對象,表中資料對象。
@interface LinViewController : UIViewController//建立表視圖的對象@property (retain, nonatomic) UITableView * mTableView;//建立數組對象,儲存表中資料@property (retain, nonatomic) NSArray * array;@end
在.m檔案中載入表視圖對象、數組對象,對協議的方法選擇性實現,樣本如下:
//釋放建立的對象- (void)dealloc{ [_mTableView release]; [_array release]; [super dealloc];}- (void)viewDidLoad{ [super viewDidLoad]; //初始化數組,存入系統的字型作為表視圖中的資料 self.array = [UIFont familyNames]; //建立初始化表視圖 self.mTableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain]; //建立一個視圖,作為表頭 UIView * pView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)]; //設定視圖的顏色 pView.backgroundColor = [UIColor blueColor]; //設定表頭視圖 self.mTableView.tableHeaderView = pView; //設定頁首的高度 self.mTableView.sectionHeaderHeight = 50; //修改行分隔線顏色 self.mTableView.separatorColor = [UIColor redColor]; //設定分割線的類型,此處設定為消失 self.mTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched; //設定委託的對象----表的委託,切記添加 self.mTableView.dataSource = self; self.mTableView.delegate = self; //把表視圖添加到當前的視圖中 [self.view addSubview:self.mTableView]; //釋放建立的臨時變數 [pView release];}#pragma mark----tableViewDataSource---協議的方法//設定分段中的行數-----必須實現- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [self.array count];}//表中每一行資料的繪製-----必須實現- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ //設定靜態變數,作為標誌 static NSString * identifer = @"identifer"; //建立TableViewCell對象,根據靜態變數標誌判斷緩衝中是否已存在 UITableViewCell * pCell = [tableView dequeueReusableCellWithIdentifier:identifer]; //判斷TableViewCell是否為空白,為空白則建立新的 if (nil == pCell) { pCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifer]; } //擷取當前要繪製的行數 NSUInteger cellRow = [indexPath row]; //根據行數找到數組中對應下標的資料 NSString * pString = [self.array objectAtIndex:cellRow]; //設定行中常值內容 pCell.textLabel.text = pString; //設定行中文本的字型 pCell.textLabel.font = [UIFont fontWithName:pString size:16]; //設定行中文本字型的顏色 pCell.textLabel.textColor = [UIColor blueColor]; //設定行中文本的注釋,根據建立TableViewCell的格式,可顯示出來 pCell.detailTextLabel.text = @"detailText"; //設定行中小表徵圖 pCell.imageView.image = [UIImage imageNamed:@"text.png"]; //設定行中標誌格式,此處為每行添加? pCell.accessoryType = UITableViewCellAccessoryCheckmark; return pCell;}//-----------可選實現的方法---------------------//為表頭設定文本- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ return @"I'm Header_Title";}//為標為設定文本,一般省略- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{ return @"I'm Footer_Title";}#pragma mark----tableViewDelegate---都是可選實現//選中某行的時候調用此方法,最常用- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ //擷取當前點擊的行數 NSUInteger row = [indexPath row]; //建立字串,儲存選中行資訊 NSString * pString = [NSString stringWithFormat:@"你選中了第%i行!",row]; //彈出警告框,顯示選中某行 UIAlertView * pAlert = [[UIAlertView alloc]initWithTitle:@"attention" message:pString delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil]; //展示警告框,釋放建立的對象 [pAlert show]; [pAlert release];}//調整行高- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 44;}//調整header 高度- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 20;}//行內容進行位移- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{ //擷取當前行數,移動相應的距離 NSUInteger row = [indexPath row]; return row;}