天天看点

ios开发系列之UITableView

UITableView基本结构

ios开发系列之UITableView

我们知道创建UITableView必须要设置代理,而我们的UITableView有两个代理:UITableViewDataSource与UITableViewDelegate,这是为了分离视图和模型.

注意:我们对单元格的所有操作本质是对数据(根据IndexPath来找到对应数据)的操作,最后刷新表[tableView reloadData];

0.

UITableViewDataSource

UITableViewDelegate

的区别:

UITableViewDataSource

协议里面东西是跟内容有关的,主要是cell的构造函数,各种属性.

UITableViewDelegate

协议里面的方法主要是操作相关的,移动编辑之类的.

官网上这句话解释的比较好:

Instead of being delegated control of the user interface, a data source is delegated control of data.

我们可以发现,delegate控制的是UI,是上层的东西;而datasource控制的是数据。他们本质都是回调,只是回调的对象不同。

官网原文:https://developer.apple.com/library/ios/#documentation/General/Conceptual/DevPedia-CocoaCore/Delegation.html

1. UITableViewDataSource 方法总结

@protocol UITableViewDataSource<NSObject>
/*****************************必须实现的两个方法***************************************/
@required  //

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;   //根据分组,返回每个分组(区)的行数  

//多行单列显示.实现者应该总是要通过设置每个单元格的reuseIdentifier与dequeueReusableCellWithIdentifier: 来从缓存队列中取出可重用的单元格,已达到重用单元格的目的 (在返回单元格的时候实现)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; //根据分组,返回每个单元格
// UITableViewCell为每个Cell提供了三个可以选择的属性,如下:
// textLabel:填写文本
// detailTextLable:稍微详细的副标题
// imageView:用来显示你cell的图片,可以通过UIImage来加载。

static NSString * cellIdentifier = @"cellIdentifier";  
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell == nil) {  
 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle                  reuseIdentifier:cellIdentifier];            
 } 
return cell;
/********************************可选的方法***************************************/
@optional 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;  // 返回分组数,默认为1  

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;   
return @"titleForHeaderInSection"; //返回节头标题
//固定字体样式.如果你想要不同的东西,使用自定义UIlabel
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
return @"titleForFooterInSection"; //返回节脚标题

/*******************************单元格编辑相关的方法**********************************/
// Editing  

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;//单个行可以选择退出具有它们的编辑属性集。如果不执行,所有的行被认为是可编辑的。

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
//设置UITableViewCell是否可以重排 本质上只是一个数组Array内的两个object对换位置Index

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView; 
//设置每节(区)的索引标题,返回一个节(区)标题表
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index;  // tell table which section corresponds to section title/index (e.g. "B",1))

// Data manipulation - insert and delete support  数据操作 ---- 插入 和 删除 本质是根据indexPath来获取数据然后操作数据最后刷新表

// After a row has the minus or plus button invoked (based on the UITableViewCellEditingStyle for the cell), the dataSource must commit the change
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
//根据编辑样式来进行相关操作
// Data manipulation - reorder / moving support   移动

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
//将一行移至另一行
@end
           

2 . UITableViewDelegate方法总结

@protocol UITableViewDelegate<NSObject, UIScrollViewDelegate>

@optional
/************************************高度设置******************************************/
// Variable height support

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; //定义cell的高度 ,可根据indexPath.section 进行更精确的配置 默认44
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;   //定义区头的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;   //定义区脚的高度

/***********************************区头区脚试图设置*************************************/
// Section header & footer information. Views are preferred over title should you decide to provide both

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;   // 自定义HeaderView 区头视图
//ios 6.0 之前的写法  如果同时设置了区头标题和区头视图,那么由于区头视图的优先级高于标题的优先级,会优先显示视图,不执行标题方法
//    UIView * view = [[UIView alloc] init];
//    view.backgroundColor = [UIColor redColor];
//    return [view autorelease];


//    ios 6.0之后 和cell的创建很像
//    1.设置view的重用标识符
     static NSString * viewID = @"viewID_identifier";
//    2.从重用池中查找
    UITableViewHeaderFooterView * headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:viewID];
//    3.不存在就创建
    if (!headerView) {
        headerView = [[[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:viewID] autorelease];
    }
    headerView.contentView.backgroundColor = [UIColor yellowColor];    
//    4.返回headerView
    return headerView;

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;//自定义Footer

/*************************************单元格被选中*************************************/
// Selection

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;
- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
// Called after the user changes the selection.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; //选中
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath ; //取消选中

/*************************************单元格编辑*************************************/
// Editing

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;//设置编辑样式
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath; //设置删除按钮的文字

- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath; //编辑单元格时,是否进行缩进,默认是YES

/*************************************单元格移动*************************************/
// Moving/reordering

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath;//可以控制移动的行最终放在哪(自动控制当前移动的单元格移动到哪个地方)               

@end