標籤:
//// ViewController.m// Simple Table//// Created by Jierism on 16/7/20.// Copyright © 2016年 Jierism. All rights reserved.//#import "ViewController.h"@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>// 聲明一個數組,用來儲存表單元的內容@property(copy,nonatomic) NSArray *dwarves;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // 初始化表單元的內容 self.dwarves = @[@"SLEEPY",@"SNEEZY",@"BASHFUL",@"HAPPY",@"DOC",@"GRUMPY",@"DOPEY",@"THORIN",@"DORIN", @"NORI",@"ORI",@"BALIN",@"DWALIN",@"FILI",@"KILI",@"OIN",@"GLOIN",@"BIFUR",@"BOFUR"];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}// 返回數組的元素個數,即cell的行數- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [self.dwarves count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ // 聲明一個靜態字串執行個體,作為鍵使用,用來表示某種表單元 // 比較複雜的表需要根據它們的內容和位置使用不同的類型的表單元,這樣就需要不同的表單元標識符來區分每一種表單元類型 static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; // 產生表單元 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier]; // 當視圖中沒有表單元時,產生表單元,數目為數組的元素個數 if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault // 設定cell的樣式,這裡為預設 reuseIdentifier:SimpleTableIdentifier]; } // 在每個表單元前面插入一個映像 UIImage *image = [UIImage imageNamed:@"star"]; // 普通狀態時顯示這個 cell.imageView.image = image; UIImage *highlightedImage = [UIImage imageNamed:@"star2"]; // 被點擊時顯示這個 cell.imageView.highlightedImage = highlightedImage; cell.textLabel.text = self.dwarves[indexPath.row]; cell.textLabel.font = [UIFont boldSystemFontOfSize:50]; // 改變字型大小 // 設定每行的細節文本,當cell的樣式設定為非預設才會顯示 if (indexPath.row < 7) { cell.detailTextLabel.text = @"Mr.Disney"; // 前面7行的內容是Mr.Disney }else{ cell.detailTextLabel.text = @"Mr.Tolkien"; // 後面的內容是 Mr.Tolkien } return cell;}// 設定行單元級縮排- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{ return indexPath.row % 4;}// 設定行不能被選中,這裡指定第一行- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.row == 0) { return nil; }else{ return indexPath; }}// 警告顯示- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSString *rowValue = self.dwarves[indexPath.row]; NSString *message = [[NSString alloc] initWithFormat:@"You selected %@",rowValue]; UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"Row Selected!" message:message preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Yes I did" style:UIAlertActionStyleDefault handler:nil]; [controller addAction:cancelAction]; [self presentViewController:controller animated:YES completion:nil]; [tableView deselectRowAtIndexPath:indexPath animated:YES];}// 修改行的高度,這裡指定除了第一行是120以外,其他行均為70r- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return indexPath.row == 0 ? 120:70;}@endView Code
以上代碼實現了一個簡單的表視圖,運行效果如
選中一個cell,表徵圖發生變化,並顯示警告彈窗,
其中這段代碼,只有當cell設定為Default以外的任意一種style才會顯示
// 設定每行的細節文本,當cell的樣式設定為非預設才會顯示 if (indexPath.row < 7) { cell.detailTextLabel.text = @"Mr.Disney"; // 前面7行的內容是Mr.Disney }else{ cell.detailTextLabel.text = @"Mr.Tolkien"; // 後面的內容是 Mr.Tolkien } return cell;
UITableViewCellStyleValueSubtitle運行效果:
(ps:為了能看清楚效果,我把字型改小了)
UITableViewCellStyleValue1運行效果:
UITableViewCellStyleValue2運行效果:
精通IOS開發-表視圖的使用