1愈诚、初始化一個(gè)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、配置一個(gè)TableView
1)返回這個(gè)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, //無(wú)分隔線
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)注冊(cè)一個(gè)包含指定標(biāo)示符TableView的Cell的nib對(duì)象
1 - (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier //兩個(gè)參數(shù)不能是nil
2 //Available in iOS 5.0 and later.```
2)注冊(cè)一個(gè)類用來(lái)創(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重要提示:使用這個(gè)方法之前必須是使用了registerNib:forCellReuseIdentifier: 或者 registerClass:forCellReuseIdentifier:方法注冊(cè)了Cell
4)使用指定的標(biāo)示符返回可重用的Cell
1 - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier
4具则、訪問(wèn)表頭和表尾的視圖
header通過(guò)下面兩個(gè)代理方法設(shè)置
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section```
footer通過(guò)下面兩個(gè)
(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
如果要做整個(gè)tableview的header和footer即纲,要通過(guò)tableview setHeaderView setFooterView```
讓表頭表尾視圖跟著tableView移動(dòng)需要把tableView的樣式設(shè)為group
1)注冊(cè)一個(gè)包含表頭或表尾的指定標(biāo)示符表視圖的nib對(duì)象
1 - (void)registerNib:(UINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifier
2 //Available in iOS 6.0 and later.```
2)注冊(cè)一個(gè)類,用來(lái)創(chuàng)建新的包含表頭或表尾的表視圖
1 - (void)registerClass:(Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifier
2 //Available in iOS 6.0 and later.```
3)返回一個(gè)指定標(biāo)識(shí)符的可重用的附帶表頭表尾的視圖
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博肋、訪問(wèn)Cell和Section
1)返回指定indexPath的Cell
1 - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath```
2)返回指定Cell的IndexPath
1 - (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell```
3)返回指定點(diǎn)的IndexPath
1 - (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point```
4)返回指定區(qū)域內(nèi)的IndexPath組成的數(shù)組
1 - (NSArray *)indexPathsForRowsInRect:(CGRect)rect```
5)返回可見(jiàn)的UITableViewCell組成的數(shù)組
1 - (NSArray *)visibleCells```
6)返回可見(jiàn)表格行的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蜂厅、滾動(dòng)TableView
1)滾動(dòng)到指定的位置
1 - (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated
1 typedef enum {
2 UITableViewScrollPositionNone,
3 UITableViewScrollPositionTop,
4 UITableViewScrollPositionMiddle,
5 UITableViewScrollPositionBottom
6 } UITableViewScrollPosition;
轉(zhuǎn)載http://www.reibang.com/p/1ddec29b83a0 感謝作者