1郭怪、初始化一個UITableView
1 - (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
1 struct CGRect {
2 CGPoint origin;
3 CGSize size;
4 };
5 typedef struct CGRect CGRect;
1 typedef enum {
2 UITableViewStylePlain, //平鋪樣式
3 UITableViewStyleGrouped //分組樣式
4 } UITableViewStyle;
2、配置一個TableView
1)返回這個TableView的樣式(只讀屬性)
1 @property(nonatomic, readonly) UITableViewStyle style
2)返回指定section內(nèi)的Cell的行數(shù)
1 - (NSInteger)numberOfRowsInSection:(NSInteger)section
當(dāng)TableView在UITableViewStylePlain下section應(yīng)該為0
3)返回TableView的section數(shù)量
1 - (NSInteger)numberOfSections
4)設(shè)置TableView中所有cell的高度
1 @property(nonatomic) CGFloat rowHeight
Apple建議我們使用代理方法tableView:heightForRowAtIndexPath:代替rowHeight方法使TableView的性能更高
5)設(shè)置TableView的分隔線的樣式
1 @property(nonatomic) UITableViewCellSeparatorStyle separatorStyle
1 typedef enum : NSInteger {
2 UITableViewCellSeparatorStyleNone, //無分隔線
3 UITableViewCellSeparatorStyleSingleLine, //單分割線
4 UITableViewCellSeparatorStyleSingleLineEtched //被侵蝕的但分隔線
5 } UITableViewCellSeparatorStyle;
6)設(shè)置TableView的分隔線顏色
1 @property(nonatomic, retain) UIColor *separatorColor
7)設(shè)置TableView的背景視圖
1 @property(nonatomic, readwrite, retain) UIView *backgroundView
8)設(shè)置TableView的分隔線偏移量
1 @property (nonatomic) UIEdgeInsets separatorInset
2 //Available in iOS 7.0 and later.
Apple的例子
1 tableView.separatorInset = UIEdgeInsetsMake(0, 3, 0, 11);
//上、左、下、右
3伊约、創(chuàng)建TableView的Cell
1)注冊一個包含指定標(biāo)示符TableView的Cell的nib對象
1 - (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier //兩個參數(shù)不能是nil
2 //Available in iOS 5.0 and later.
2)注冊一個類用來創(chuàng)建新的Cell
1 - (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier
2 //Available in iOS 6.0 and later.
3)使用指定的標(biāo)示符返回可重用的Cell
1 - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath
2 //Available in iOS 6.0 and later.
Apple重要提示:使用這個方法之前必須是使用了registerNib:forCellReuseIdentifier: 或者 registerClass:forCellReuseIdentifier:方法注冊了Cell
4)使用指定的標(biāo)示符返回可重用的Cell
1 - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier
4词顾、訪問表頭和表尾的視圖
header通過下面兩個代理方法設(shè)置
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
footer通過下面兩個
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
如果要做整個tableview的header和footer,要通過tableview setHeaderView setFooterView```
讓表頭表尾視圖跟著tableView移動需要把tableView的樣式設(shè)為`group`
1)注冊一個包含表頭或表尾的指定標(biāo)示符表視圖的nib對象
1 - (void)registerNib:(UINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifier
2 //Available in iOS 6.0 and later.```
2)注冊一個類碱妆,用來創(chuàng)建新的包含表頭或表尾的表視圖
1 - (void)registerClass:(Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifier
2 //Available in iOS 6.0 and later.```
3)返回一個指定標(biāo)識符的可重用的附帶表頭表尾的視圖
1 - (id)dequeueReusableHeaderFooterViewWithIdentifier:(NSString *)identifier
2 //Available in iOS 6.0 and later.```
4)設(shè)置或返回該表格的表頭視圖
1 @property(nonatomic, retain) UIView *tableHeaderView
5)設(shè)置或返回該表格的表尾視圖
1 @property(nonatomic, retain) UIView *tableFooterView
6)設(shè)置或返回該表格的表頭高度
1 @property(nonatomic) CGFloat sectionHeaderHeight```
7)設(shè)置或返回該表格的表尾高度
1 @property(nonatomic) CGFloat sectionFooterHeight```
8)返回指定section的表頭視圖
1 - (UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section
2 //Available in iOS 6.0 and later.```
9)返回指定section的表尾視圖
1 - (UITableViewHeaderFooterView *)footerViewForSection:(NSInteger)section
2 //Available in iOS 6.0 and later.```
5、訪問Cell和Section
1)返回指定indexPath的Cell
1 - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath```
2)返回指定Cell的IndexPath
1 - (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell```
3)返回指定點的IndexPath
1 - (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point```
4)返回指定區(qū)域內(nèi)的IndexPath組成的數(shù)組
1 - (NSArray *)indexPathsForRowsInRect:(CGRect)rect```
5)返回可見的UITableViewCell組成的數(shù)組
1 - (NSArray *)visibleCells```
6)返回可見表格行的IndexPath組成的數(shù)組
1 - (NSArray *)indexPathsForVisibleRows```
6昔驱、估算元素的高度
1)設(shè)置表格行的估算高度以改善性能
1 @property (nonatomic) CGFloat estimatedRowHeight
2 //The default value is 0, which means there is no estimate.
3 //Available in iOS 7.0 and later.```
2)設(shè)置Section頭的估算高度以改善性能
1 @property(nonatomic) CGFloat estimatedSectionHeaderHeight
2 //The default value is 0, which means there is no estimate.
3 //Available in iOS 7.0 and later.```
3)設(shè)置Section尾的古都按高度以改善性能
1 @property(nonatomic) CGFloat estimatedSectionFooterHeight
2 //The default value is 0, which means there is no estimate.
3 //Available in iOS 7.0 and later.```
###7疹尾、滾動TableView
1)滾動到指定的位置
1 - (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated```
1 typedef enum {
2 UITableViewScrollPositionNone,
3 UITableViewScrollPositionTop,
4 UITableViewScrollPositionMiddle,
5 UITableViewScrollPositionBottom
6 } UITableViewScrollPosition;```