tableView的常見屬性
cell的常見屬性
一、一般情況
1 #import "ViewController.h" 2 3 @interface ViewController ()<UITableViewDataSource,UITableViewDelegate> 4 5 @property (strong, nonatomic) UITableView *tableview; 6 7 @property (strong, nonatomic) NSArray *dataSource; 8 9 @end 10 11 /* 12 UITableView 13 表格檢視:兩個協議,兩個代理,四個方法 14 15 兩個協議: 16 1.資料來源協議:(提供表格資料,提供表格內容)UITableViewDataSource 17 2.執行代理協議:(提供表格操作方法)UITableViewDelegate 18 */ 19 20 21 @implementation ViewController 22 23 #pragma mark - LifeCircle 24 - (void)viewDidLoad { 25 [super viewDidLoad]; 26 27 [self.view setBackgroundColor:[UIColor groupTableViewBackgroundColor]]; 28 29 self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, 375, 400) style:UITableViewStyleGrouped]; 30 31 self.tableview.delegate = self; 32 self.tableview.dataSource = self; 33 34 [self.view addSubview:self.tableview]; 35 36 NSArray *arr1 = @[@"芃,你好1",@"芃,你好2",@"芃,你好3",@"芃,你好4",@"芃,你好5"]; 37 NSArray *arr2 = @[@"芃,你好6",@"芃,你好7",@"芃,你好8",@"芃,你好9"]; 38 self.dataSource = @[arr1,arr2]; 39 40 41 } 42 43 #pragma mark - UITableViewDataSource 44 //組數 45 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 46 { 47 return [self.dataSource count]; 48 } 49 50 //設定每組行數 51 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 52 { 53 return [[self.dataSource objectAtIndex:section] count]; 54 } 55 56 // 設定[每一個]單元格方法 57 // 1.設定複用[單元格]ID 58 // 2.設定複用 59 // 3.建立單元格 60 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 61 { 62 // 1.設定複用[單元格]ID 63 static NSString *cellIdentifier = @"cellIdentifier"; 64 // 2.設定複用 65 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 66 // 3.建立單元格 67 if (!cell) { 68 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 69 } 70 cell.textLabel.text = [[self.dataSource objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; 71 cell.imageView.image = [UIImage imageNamed:@"1.jpg"]; 72 cell.detailTextLabel.text = @"張芃芃的資訊"; 73 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 74 75 76 return cell; 77 } 78 79 #pragma mark - UITableViewDelegate 80 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 81 { 82 //拿到點選的那一行 83 UITableViewCell *selectCell = [tableView cellForRowAtIndexPath:indexPath]; 84 85 NSString *titleInRow = [NSString stringWithFormat:@"%@",selectCell.textLabel.text]; 86 87 // NSString *titleInRow = [NSString stringWithFormat:@"%@",[[self.dataSource objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]]; 88 89 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"問候" message:titleInRow delegate:self cancelButtonTitle:@"你好" otherButtonTitles: nil]; 90 [alert show]; 91 } 92 93 @end
二、通過載入plist檔案顯示分組資料
1 #import <Foundation/Foundation.h> 2 3 @interface Group : NSObject 4 //組標題 5 @property (copy, nonatomic) NSString *title; 6 //組描述 7 @property (copy, nonatomic) NSString *desc; 8 //汽車品牌資訊 9 @property (strong, nonatomic) NSArray *cars; 10 11 - (instancetype) initWithDict:(NSDictionary *)dict; 12 + (instancetype) groupWithDict:(NSDictionary *)dict; 13 14 @end 15 16 17 #import "Group.h" 18 19 @implementation Group 20 21 - (instancetype)initWithDict:(NSDictionary *)dict 22 { 23 self = [super init]; 24 if (self) { 25 // self.title = dict[@"title"]; 26 // self.desc = dict[@"desc"]; 27 // self.cars = dict[@"cars"]; 28 [self setValuesForKeysWithDictionary:dict]; 29 } 30 return self; 31 } 32 33 + (instancetype)groupWithDict:(NSDictionary *)dict 34 { 35 return [[self alloc] initWithDict:dict]; 36 } 37 38 @end 39 40 41 #import "ViewController.h" 42 #import "Group.h" 43 44 @interface ViewController ()<UITableViewDataSource> 45 46 @property (strong, nonatomic) NSArray *groups; 47 @property (strong, nonatomic) UITableView *tableView; 48 49 @end 50 51 @implementation ViewController 52 53 - (void)viewDidLoad { 54 [super viewDidLoad]; 55 self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds]; 56 self.tableView.dataSource = self; 57 [self.view addSubview:self.tableView]; 58 59 } 60 61 #pragma mark - 資料來源協議 62 63 //設定組數 64 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 65 { 66 return [self.groups count]; 67 } 68 //設定每組行數 69 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 70 { 71 Group *group = self.groups[section]; 72 return group.cars.count; 73 74 } 75 //設定每組每行內容 76 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 77 { 78 //1.設定複用單元格ID 79 static NSString *cellIdentifier = @"cellIdentifier"; 80 //2.設定複用 81 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 82 //3.建立單元格 83 if (!cell) { 84 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
// 3.1獲取模型資料 86 //獲取模型 87 Group *group = self.groups[indexPath.section]; 88 //獲取對應的汽車品牌 89 NSString *brand = group.cars[indexPath.row]; 90 // 3.2把模型中的資料設定給單元格中的子控制元件 91 cell.textLabel.text = brand; 92 93 94 return cell; 95 } 96 97 //組標題 98 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 99 { 100 Group *group = self.groups[section]; 101 return group.title; 102 } 103 //設定組尾 104 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section 105 { 106 Group *group = self.groups[section]; 107 return group.desc; 108 } 109 110 111 #pragma mark - 懶載入 112 - (NSArray *)groups 113 { 114 if (_groups == nil) { 115 // 1.找到plist檔案的路徑 116 NSString *path = [[NSBundle mainBundle] pathForResource:@"cars_simple.plist" ofType:nil]; 117 // 2.載入plist檔案 118 NSArray *array = [NSArray arrayWithContentsOfFile:path]; 119 // 3.字典轉模型 120 NSMutableArray *arrayModel = [NSMutableArray array]; 121 // 4.遍歷字典陣列中的每個字典,把每個字典轉成模型,把模型放到arrayModel 122 for (NSDictionary *tempDict in array) { 123 //建立模型物件 124 Group *model = [[Group alloc] initWithDict:tempDict]; 125 [arrayModel addObject:model]; 126 } 127 _groups =arrayModel; 128 129 } 130 return _groups; 131 } 132 133 #pragma mark - 設定狀態列文字的顏色 134 - (UIStatusBarStyle)preferredStatusBarStyle 135 { 136 return UIStatusBarStyleLightContent; 137 } 138 139 #pragma mark - 隱藏狀態列 140 - (BOOL)prefersStatusBarHidden 141 { 142 return YES; 143 } 144 145 - (void)didReceiveMemoryWarning { 146 [super didReceiveMemoryWarning]; 147 // Dispose of any resources that can be recreated. 148 } 149 150 @end
三、通過載入plist檔案展示單組資料
1 #import <Foundation/Foundation.h> 2 3 @interface HMLOL : NSObject 4 @property (nonatomic, copy) NSString *icon; 5 @property (nonatomic, copy) NSString *name; 6 @property (nonatomic, copy) NSString *intro; 7 8 - (instancetype)initWithDict:(NSDictionary *)dict; 9 + (instancetype)lolWithDict:(NSDictionary *)dict; 10 @end 11 12 #import "HMLOL.h" 13 14 @implementation HMLOL 15 - (instancetype)initWithDict:(NSDictionary *)dict 16 { 17 self = [super init]; 18 if (self) { 19 [self setValuesForKeysWithDictionary:dict]; 20 } 21 return self; 22 } 23 24 + (instancetype)lolWithDict:(NSDictionary *)dict { 25 return [[self alloc] initWithDict:dict]; 26 } 27 @end 28 29 30 #import "ViewController.h" 31 #import "HMLOL.h" 32 @interface ViewController ()<UITableViewDataSource> 33 @property (weak, nonatomic) IBOutlet UITableView *tableView; 34 // 存放所有模型資料 35 @property (nonatomic, strong) NSArray *heroes; 36 @end 37 38 @implementation ViewController 39 40 - (void)viewDidLoad { 41 [super viewDidLoad]; 42 43 // 1.設定資料來源 44 self.tableView.dataSource = self; 45 } 46 47 48 // 49 //- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 50 // return 1; 51 //} 52 53 // 返回多少行 54 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 55 return self.heroes.count; 56 } 57 58 59 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 60 // 1.建立cell 61 UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil]; 62 63 // 2.取出模型中的資料設定給cell 64 HMLOL *hero = self.heroes[indexPath.row]; 65 // cell內部顯示內容的三個子控制元件 66 cell.textLabel.text = hero.name; 67 cell.imageView.image = [UIImage imageNamed:hero.icon]; 68 cell.detailTextLabel.text = hero.intro; 69 70 // 設定輔助檢視的樣式 71 // cell.accessoryType = UITableViewCellAccessoryNone; 72 // 設定自定義的輔助檢視 73 // cell.accessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd]; 74 // 3.返回給tabelView 75 return cell; 76 } 77 78 79 #pragma mark - 懶載入 80 - (NSArray *)heroes { 81 if (_heroes == nil) { 82 NSArray *dictArr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil]]; 83 NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:dictArr.count]; 84 for (NSDictionary *dict in dictArr) { 85 HMLOL *hero = [HMLOL lolWithDict:dict]; 86 [arrM addObject:hero]; 87 } 88 _heroes = arrM; 89 } 90 return _heroes; 91 } 92 @end
四、汽車品牌展示
1 #import <UIKit/UIKit.h> 2 3 @interface BWCar : UIView 4 @property (nonatomic, copy) NSString *name; 5 @property (nonatomic, copy) NSString *icon; 6 7 -(instancetype) initWithDict:(NSDictionary *)dict; 8 +(instancetype) carWithDict:(NSDictionary *)dict; 9 10 @end 11 12 #import "BWCar.h" 13 14 @implementation BWCar 15 16 - (instancetype)initWithDict:(NSDictionary *)dict 17 { 18 if (self = [super init]) { 19 [self setValuesForKeysWithDictionary:dict]; 20 } 21 return self; 22 } 23 24 + (instancetype)carWithDict:(NSDictionary *)dict 25 { 26 return [[self alloc] initWithDict:dict]; 27 } 28 29 @end 30 31 32 33 #import <UIKit/UIKit.h> 34 35 @interface BWGroup : UIView 36 37 @property (nonatomic, copy) NSString *title; 38 @property (nonatomic, strong) NSArray *cars; 39 40 - (instancetype) initWithDict:(NSDictionary *)dict; 41 + (instancetype) groupWithDict:(NSDictionary *)dict; 42 43 @end 44 45 #import "BWGroup.h" 46 #import "BWCar.h" 47 48 @implementation BWGroup 49 50 - (instancetype)initWithDict:(NSDictionary *)dict 51 { 52 if (self = [super init]) { 53 [self setValuesForKeysWithDictionary:dict]; 54 //當有模型巢狀的時候需要手動把字典轉模型 55 //模型陣列 56 NSMutableArray *arrayModels = [NSMutableArray array]; 57 //字典轉模型 58 for (NSDictionary *tempDict in self.cars) { 59 BWCar *model = [[BWCar alloc] initWithDict:tempDict]; 60 [arrayModels addObject:model]; 61 } 62 self.cars = arrayModels; 63 } 64 65 return self; 66 } 67 68 + (instancetype)groupWithDict:(NSDictionary *)dict 69 { 70 return [[self alloc] initWithDict:dict]; 71 } 72 73 @end 74 75 #import "ViewController.h" 76 #import "BWCar.h" 77 #import "BWGroup.h" 78 79 80 @interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate> 81 82 @property (nonatomic, strong) NSArray *groups; 83 @property (nonatomic, strong) UITableView *tableView; 84 @property (nonatomic, strong) NSIndexPath *indexPath; 85 86 @end 87 88 @implementation ViewController 89 90 #pragma mark - 懶載入資料 91 - (NSArray *)groups 92 { 93 if (_groups == nil) { 94 95 NSString *path = [[NSBundle mainBundle] pathForResource:@"cars_total.plist" ofType:nil]; 96 NSArray *arr = [NSArray arrayWithContentsOfFile:path]; 97 NSMutableArray *arrayModels = [NSMutableArray array]; 98 for (NSDictionary *dict in arr) { 99 BWGroup *group = [BWGroup groupWithDict:dict]; 100 [arrayModels addObject:group]; 101 } 102 _groups = arrayModels; 103 } 104 return _groups; 105 } 106 107 - (void)viewDidLoad { 108 [super viewDidLoad]; 109 110 self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped]; 111 self.tableView.dataSource =self; 112 self.tableView.delegate =self; 113 114 [self.view addSubview:self.tableView]; 115 116 117 } 118 #pragma mark - 資料來源方法 119 //載入陣列數 120 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 121 { 122 return self.groups.count; 123 } 124 //載入每組行數 125 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 126 { 127 BWGroup *group = [self.groups objectAtIndex:section]; 128 return group.cars.count; 129 } 130 131 //載入單元格資料 132 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 133 { 134 // 1.建立單元格 135 136 // 1.1 宣告重用ID 137 static NSString *cellIdentifier = @"cellIdentifier"; 138 // 1.2 根據重用ID去快取池中取對應的cell物件 139 UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 140 // 1.3 如果沒有取到,就建立一個cell 141 if (!cell) { 142 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; 143 } 144 // 2.設定單元格內容 145 146 // 2.1 取資料模型 147 BWGroup *group = self.groups[indexPath.section]; 148 BWCar *car = group.cars[indexPath.row]; 149 150 // 2.2 載入單元格內容 151 cell.textLabel.text = car.name; 152 cell.imageView.image = [UIImage imageNamed:car.icon]; 153 154 // 3.返回單元格 155 return cell; 156 } 157 158 //載入組標題 159 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 160 { 161 BWGroup *group = [self.groups objectAtIndex:section]; 162 return group.title; 163 } 164 165 //設定右側索引蘭 166 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 167 { 168 return [self.groups valueForKey:@"title"]; 169 } 170 171 172 173 #pragma mark - 代理方法 174 //監聽行被選中的代理方法 175 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 176 { 177 //引用indexPath 178 self.indexPath = indexPath; 179 180 //獲取當前被選中行的車的物件 181 BWGroup *group = self.groups[indexPath.section]; 182 BWCar *car = group.cars[indexPath.row]; 183 184 //建立一個對話方塊物件 185 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"編輯" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定",nil]; 186 187 188 189 //修改UIAlertView的樣式 190 alert.alertViewStyle = UIAlertViewStylePlainTextInput; 191 //獲取那個文字框,並且設定文字框中的文字為car.name 192 [alert textFieldAtIndex:0].text = car.name; 193 //顯示對話方塊 194 [alert show]; 195 } 196 //UIAlertView的按鈕被點選了,就會執行 197 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 198 { 199 //表示點選的是"確定" 200 if (buttonIndex == 1) { 201 // 更新資料 202 // 1.獲取使用者文字框中的內容 203 NSString *name = [alertView textFieldAtIndex:0].text; 204 205 // 2.找到對應的車模型 206 // BWCar *car = self.groups[alertView.tag]; 207 BWGroup *group = self.groups[self.indexPath.section]; 208 BWCar *car = group.cars[self.indexPath.row]; 209 // NSLog(@"%@",self.indexPath); 210 211 // 3.修改車模型的name 212 car.name = name; 213 214 // 4.重新整理tableView(重新呼叫UITableView的資料來源物件中的資料來源方法) 215 216 // reloadData重新整理整個tableView 217 // [self.tableView reloadData]; 218 219 //區域性重新整理 220 //建立一個行物件 221 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.indexPath.row inSection:self.indexPath.section]; 222 [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; 223 } 224 } 225 226 @end
五、UITableView的API
1 // 2 // UITableView.h 3 // UIKit 4 // 5 // Copyright (c) 2005-2014 Apple Inc. All rights reserved. 6 // 7 8 #import <Foundation/Foundation.h> 9 #import <CoreGraphics/CoreGraphics.h> 10 #import <UIKit/UIScrollView.h> 11 #import <UIKit/UISwipeGestureRecognizer.h> 12 #import <UIKit/UITableViewCell.h> 13 #import <UIKit/UIKitDefines.h> 14 15 typedef NS_ENUM(NSInteger, UITableViewStyle) { 16 UITableViewStylePlain, // regular table view 17 UITableViewStyleGrouped // preferences style table view 18 }; 19 20 typedef NS_ENUM(NSInteger, UITableViewScrollPosition) { 21 UITableViewScrollPositionNone, 22 UITableViewScrollPositionTop, 23 UITableViewScrollPositionMiddle, 24 UITableViewScrollPositionBottom 25 }; // scroll so row of interest is completely visible at top/center/bottom of view 26 27 typedef NS_ENUM(NSInteger, UITableViewRowAnimation) { 28 UITableViewRowAnimationFade, 29 UITableViewRowAnimationRight, // slide in from right (or out to right) 30 UITableViewRowAnimationLeft, 31 UITableViewRowAnimationTop, 32 UITableViewRowAnimationBottom, 33 UITableViewRowAnimationNone, // available in iOS 3.0 34 UITableViewRowAnimationMiddle, // available in iOS 3.2. attempts to keep cell centered in the space it will/did occupy 35 UITableViewRowAnimationAutomatic = 100 // available in iOS 5.0. chooses an appropriate animation style for you 36 }; 37 38 // Including this constant string in the array of strings returned by sectionIndexTitlesForTableView: will cause a magnifying glass icon to be displayed at that location in the index. 39 // This should generally only be used as the first title in the index. 40 UIKIT_EXTERN NSString *const UITableViewIndexSearch NS_AVAILABLE_IOS(3_0); 41 42 // Returning this value from tableView:heightForHeaderInSection: or tableView:heightForFooterInSection: results in a height that fits the value returned from 43 // tableView:titleForHeaderInSection: or tableView:titleForFooterInSection: if the title is not nil. 44 UIKIT_EXTERN const CGFloat UITableViewAutomaticDimension NS_AVAILABLE_IOS(5_0); 45 46 @class UITableView; 47 @class UINib; 48 @protocol UITableViewDataSource; 49 @class UILongPressGestureRecognizer; 50 @class UITableViewHeaderFooterView; 51 @class UIRefreshControl; 52 @class UIVisualEffect; 53 54 typedef NS_ENUM(NSInteger, UITableViewRowActionStyle) { 55 UITableViewRowActionStyleDefault = 0, 56 UITableViewRowActionStyleDestructive = UITableViewRowActionStyleDefault, 57 UITableViewRowActionStyleNormal 58 } NS_ENUM_AVAILABLE_IOS(8_0); 59 60 NS_CLASS_AVAILABLE_IOS(8_0) @interface UITableViewRowAction : NSObject <NSCopying> 61 62 + (instancetype)rowActionWithStyle:(UITableViewRowActionStyle)style title:(NSString *)title handler:(void (^)(UITableViewRowAction *action, NSIndexPath *indexPath))handler; 63 64 @property (nonatomic, readonly) UITableViewRowActionStyle style; 65 @property (nonatomic, copy) NSString *title; 66 @property (nonatomic, copy) UIColor *backgroundColor; // default background color is dependent on style 67 @property (nonatomic, copy) UIVisualEffect* backgroundEffect; 68 69 @end 70 71 //_______________________________________________________________________________________________________________ 72 // this represents the display and behaviour of the cells. 73 74 @protocol UITableViewDelegate<NSObject, UIScrollViewDelegate> 75 76 @optional 77 78 // Display customization 79 80 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath; 81 - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0); 82 - (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0); 83 - (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath NS_AVAILABLE_IOS(6_0); 84 - (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0); 85 - (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0); 86 87 // Variable height support 88 89 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; 90 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section; 91 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section; 92 93 // Use the estimatedHeight methods to quickly calcuate guessed values which will allow for fast load times of the table. 94 // If these methods are implemented, the above -tableView:heightForXXX calls will be deferred until views are ready to be displayed, so more expensive logic can be placed there. 95 - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(7_0); 96 - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0); 97 - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0); 98 99 // Section header & footer information. Views are preferred over title should you decide to provide both 100 101 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; // custom view for header. will be adjusted to default or specified header height 102 - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section; // custom view for footer. will be adjusted to default or specified footer height 103 104 // Accessories (disclosures). 105 106 - (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath NS_DEPRECATED_IOS(2_0, 3_0); 107 - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath; 108 109 // Selection 110 111 // -tableView:shouldHighlightRowAtIndexPath: is called when a touch comes down on a row. 112 // Returning NO to that message halts the selection process and does not cause the currently selected row to lose its selected look while the touch is down. 113 - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0); 114 - (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0); 115 - (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0); 116 117 // Called before the user changes the selection. Return a new indexPath, or nil, to change the proposed selection. 118 - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath; 119 - (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0); 120 // Called after the user changes the selection. 121 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; 122 - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0); 123 124 // Editing 125 126 // Allows customization of the editingStyle for a particular cell located at 'indexPath'. If not implemented, all editable cells will have UITableViewCellEditingStyleDelete set for them when the table has editing property set to YES. 127 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath; 128 - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0); 129 - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0); // supercedes -tableView:titleForDeleteConfirmationButtonForRowAtIndexPath: if return value is non-nil 130 131 // Controls whether the background is indented while editing. If not implemented, the default is YES. This is unrelated to the indentation level below. This method only applies to grouped style table views. 132 - (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath; 133 134 // The willBegin/didEnd methods are called whenever the 'editing' property is automatically changed by the table (allowing insert/delete/move). This is done by a swipe activating a single row 135 - (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath; 136 - (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath; 137 138 // Moving/reordering 139 140 // Allows customization of the target row for a particular row as it is being moved/reordered 141 - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath; 142 143 // Indentation 144 145 - (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath; // return 'depth' of row for hierarchies 146 147 // Copy/Paste. All three methods must be implemented by the delegate. 148 149 - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(5_0); 150 - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender NS_AVAILABLE_IOS(5_0); 151 - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender NS_AVAILABLE_IOS(5_0); 152 153 @end 154 155 UIKIT_EXTERN NSString *const UITableViewSelectionDidChangeNotification; 156 157 158 //_______________________________________________________________________________________________________________ 159 160 NS_CLASS_AVAILABLE_IOS(2_0) @interface UITableView : UIScrollView <NSCoding> 161 162 - (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style; // must specify style at creation. -initWithFrame: calls this with UITableViewStylePlain 163 164 @property (nonatomic, readonly) UITableViewStyle style; 165 @property (nonatomic, assign) id <UITableViewDataSource> dataSource; 166 @property (nonatomic, assign) id <UITableViewDelegate> delegate; 167 @property (nonatomic) CGFloat rowHeight; // will return the default value if unset 168 @property (nonatomic) CGFloat sectionHeaderHeight; // will return the default value if unset 169 @property (nonatomic) CGFloat sectionFooterHeight; // will return the default value if unset 170 @property (nonatomic) CGFloat estimatedRowHeight NS_AVAILABLE_IOS(7_0); // default is 0, which means there is no estimate 171 @property (nonatomic) CGFloat estimatedSectionHeaderHeight NS_AVAILABLE_IOS(7_0); // default is 0, which means there is no estimate 172 @property (nonatomic) CGFloat estimatedSectionFooterHeight NS_AVAILABLE_IOS(7_0); // default is 0, which means there is no estimate 173 @property (nonatomic) UIEdgeInsets separatorInset NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR; // allows customization of the frame of cell separators 174 175 @property(nonatomic, readwrite, retain) UIView *backgroundView NS_AVAILABLE_IOS(3_2); // the background view will be automatically resized to track the size of the table view. this will be placed as a subview of the table view behind all cells and headers/footers. default may be non-nil for some devices. 176 177 // Data 178 179 - (void)reloadData; // reloads everything from scratch. redisplays visible rows. because we only keep info about visible rows, this is cheap. will adjust offset if table shrinks 180 - (void)reloadSectionIndexTitles NS_AVAILABLE_IOS(3_0); // reloads the index bar. 181 182 // Info 183 184 - (NSInteger)numberOfSections; 185 - (NSInteger)numberOfRowsInSection:(NSInteger)section; 186 187 - (CGRect)rectForSection:(NSInteger)section; // includes header, footer and all rows 188 - (CGRect)rectForHeaderInSection:(NSInteger)section; 189 - (CGRect)rectForFooterInSection:(NSInteger)section; 190 - (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath; 191 192 - (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point; // returns nil if point is outside of any row in the table 193 - (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell; // returns nil if cell is not visible 194 - (NSArray *)indexPathsForRowsInRect:(CGRect)rect; // returns nil if rect not valid 195 196 - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath; // returns nil if cell is not visible or index path is out of range 197 - (NSArray *)visibleCells; 198 - (NSArray *)indexPathsForVisibleRows; 199 - (UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section NS_AVAILABLE_IOS(6_0); 200 - (UITableViewHeaderFooterView *)footerViewForSection:(NSInteger)section NS_AVAILABLE_IOS(6_0); 201 202 - (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated; 203 - (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated; 204 205 // Row insertion/deletion/reloading. 206 207 - (void)beginUpdates; // allow multiple insert/delete of rows and sections to be animated simultaneously. Nestable 208 - (void)endUpdates; // only call insert/delete/reload calls or change the editing state inside an update block. otherwise things like row count, etc. may be invalid. 209 210 - (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation; 211 - (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation; 212 - (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0); 213 - (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection NS_AVAILABLE_IOS(5_0); 214 215 - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation; 216 - (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation; 217 - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0); 218 - (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath NS_AVAILABLE_IOS(5_0); 219 220 // Editing. When set, rows show insert/delete/reorder controls based on data source queries 221 222 @property (nonatomic, getter=isEditing) BOOL editing; // default is NO. setting is not animated. 223 - (void)setEditing:(BOOL)editing animated:(BOOL)animated; 224 225 @property (nonatomic) BOOL allowsSelection NS_AVAILABLE_IOS(3_0); // default is YES. Controls whether rows can be selected when not in editing mode 226 @property (nonatomic) BOOL allowsSelectionDuringEditing; // default is NO. Controls whether rows can be selected when in editing mode 227 @property (nonatomic) BOOL allowsMultipleSelection NS_AVAILABLE_IOS(5_0); // default is NO. Controls whether multiple rows can be selected simultaneously 228 @property (nonatomic) BOOL allowsMultipleSelectionDuringEditing NS_AVAILABLE_IOS(5_0); // default is NO. Controls whether multiple rows can be selected simultaneously in editing mode 229 230 // Selection 231 232 - (NSIndexPath *)indexPathForSelectedRow; // returns nil or index path representing section and row of selection. 233 - (NSArray *)indexPathsForSelectedRows NS_AVAILABLE_IOS(5_0); // returns nil or a set of index paths representing the sections and rows of the selection. 234 235 // Selects and deselects rows. These methods will not call the delegate methods (-tableView:willSelectRowAtIndexPath: or tableView:didSelectRowAtIndexPath:), nor will it send out a notification. 236 - (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition; 237 - (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated; 238 239 // Appearance 240 241 @property (nonatomic) NSInteger sectionIndexMinimumDisplayRowCount; // show special section index list on right when row count reaches this value. default is 0 242 @property (nonatomic, retain) UIColor *sectionIndexColor NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR; // color used for text of the section index 243 @property (nonatomic, retain) UIColor *sectionIndexBackgroundColor NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR; // the background color of the section index while not being touched 244 @property (nonatomic, retain) UIColor *sectionIndexTrackingBackgroundColor NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR; // the background color of the section index while it is being touched 245 246 @property (nonatomic) UITableViewCellSeparatorStyle separatorStyle; // default is UITableViewCellSeparatorStyleSingleLine 247 @property (nonatomic, retain) UIColor *separatorColor UI_APPEARANCE_SELECTOR; // default is the standard separator gray 248 @property (nonatomic, copy) UIVisualEffect *separatorEffect NS_AVAILABLE_IOS(8_0) UI_APPEARANCE_SELECTOR; // effect to apply to table separators 249 250 @property (nonatomic, retain) UIView *tableHeaderView; // accessory view for above row content. default is nil. not to be confused with section header 251 @property (nonatomic, retain) UIView *tableFooterView; // accessory view below content. default is nil. not to be confused with section footer 252 253 - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier; // Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one. 254 - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0); // newer dequeue method guarantees a cell is returned and resized properly, assuming identifier is registered 255 - (id)dequeueReusableHeaderFooterViewWithIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0); // like dequeueReusableCellWithIdentifier:, but for headers/footers 256 257 // Beginning in iOS 6, clients can register a nib or class for each cell. 258 // If all reuse identifiers are registered, use the newer -dequeueReusableCellWithIdentifier:forIndexPath: to guarantee that a cell instance is returned. 259 // Instances returned from the new dequeue method will also be properly sized when they are returned. 260 - (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(5_0); 261 - (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0); 262 263 - (void)registerNib:(UINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0); 264 - (void)registerClass:(Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0); 265 266 @end 267 268 //_______________________________________________________________________________________________________________ 269 // this protocol represents the data model object. as such, it supplies no information about appearance (including the cells) 270 271 @protocol UITableViewDataSource<NSObject> 272 273 @required 274 275 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; 276 277 // Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier: 278 // Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls) 279 280 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 281 282 @optional 283 284 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; // Default is 1 if not implemented 285 286 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; // fixed font style. use custom view (UILabel) if you want something different 287 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section; 288 289 // Editing 290 291 // Individual rows can opt out of having the -editing property set for them. If not implemented, all rows are assumed to be editable. 292 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath; 293 294 // Moving/reordering 295 296 // Allows the reorder accessory view to optionally be shown for a particular row. By default, the reorder control will be shown only if the datasource implements -tableView:moveRowAtIndexPath:toIndexPath: 297 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath; 298 299 // Index 300 301 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView; // return list of section titles to display in section index view (e.g. "ABCD...Z#") 302 - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index; // tell table which section corresponds to section title/index (e.g. "B",1)) 303 304 // Data manipulation - insert and delete support 305 306 // After a row has the minus or plus button invoked (based on the UITableViewCellEditingStyle for the cell), the dataSource must commit the change 307 // Not called for edit actions using UITableViewRowAction - the action's handler will be invoked instead 308 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath; 309 310 // Data manipulation - reorder / moving support 311 312 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath; 313 314 @end 315 316 //_______________________________________________________________________________________________________________ 317 318 // This category provides convenience methods to make it easier to use an NSIndexPath to represent a section and row 319 @interface NSIndexPath (UITableView) 320 321 + (NSIndexPath *)indexPathForRow:(NSInteger)row inSection:(NSInteger)section; 322 323 @property(nonatomic,readonly) NSInteger section; 324 @property(nonatomic,readonly) NSInteger row; 325 326 @end