参考:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/Reference/Reference.html <UITableView Class Reference> + UITableView.h
简介:
--一个@的实例意味着展示和编辑分层列表的信息。一个tableview在一栏中展示了一系列item,它是UIScrollview的子类,允许用户在列表上滑动,但只能是垂直方向。
--tableview有俩种样式,见下图:
==================datasource方法==================
@required
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
@optional
// Default is 1 if not implemented
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; //每个section的title
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section; //每个section的footer
// Editing/Moving or Reordering
// 不同的row可以有同的-editing属性。当row的editing属性为NO,则它不可编辑(tableview setEditing改变的时候它没影响);为YES时,可编辑,可以有不同的editingStyle,详见"editingStyleForRowAtIndexPath方法"。Default is YES;
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
// 不同的row是否可以移动。当tableview处于editing状态时:如果某个row返回值为NO,则它的reorder accessory view不会显示,不可移动;如果某个row的返回值为YES的时候,且datasource有实现-tableView:moveRowAtIndexPath:toIndexPath:方法,则显示该row的reorder accessory view 可以移动,否则还是不显示reorder accessory view,不可移动;
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
// Index
//返回list of section titles to display in section index view (e.g. "ABCD...Z#")
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;
// which section corresponds to sectionIndexTitle,当点击某个sectionIndexTitle时候,返回某个section的值,tableview就移动到该section;
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index;
// Data manipulation - insert and delete / reorder or moving support
// 当tableview处于editing状态时,如果row可编辑,则会has the minus or plus button invoked(based on the UITableViewCellEditingStyle for the cell)。当点击minus or plus button的时候,就会调用该方法(如果是minus button,需要点击到随后的delete才会触发方法);
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
// 当tableview处于editing状态,并且row可以canMove,如果拖动某row换行,则调用改方法;
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
@end
==================
delegate方法==================
@optional
// Display customization
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath NS_AVAILABLE_IOS(6_0);
- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
- (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
// Variable height support
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
// tableview在每次reload data时都会要所有cell的高度,这非常影响程序性能,使用estimatedHeight methods to quickly calcuate guessed values 去快速加载tableview
// 原理:If