iOS开发_ UITableView的协议方法

  • 需遵守协议 UITableViewDataSource, UITableViewDelegate,并设置代理

UITableViewDelegate 继承自 UIScrollViewDelegate

@protocol UITableViewDelegate<NSObject, UIScrollViewDelegate>

1、UITableViewDataSource 和 UITableViewDelegate 协议方法

  • 1.1 分段、行 设置

// 设置分段数,设置 tableView 有多少个分段
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

	return myDataArray.count;
}

// 设置行数,设置 tableView 中每段中有多少行,section 就是第几分段
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

	return [[myDataArray objectAtIndex:section] count];
}

// 设置行高 ,默认为 44
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

	return 60;
}

// 设置估计行高   
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {

	/*
	只要返回了估计高度,那么就会先调用 tableView:cellForRowAtIndexPath: 方法创建 cell,
	再调用 tableView:heightForRowAtIndexPath: 方法获取 cell 的真实高度,
	并且显示一个 cell,调用一次 tableView:heightForRowAtIndexPath: 方法。

	如果不返回估计高度,会先调用 tableView:heightForRowAtIndexPath: 方法,
	再调用 tableView:heightForRowAtIndexPath: 方法,
	并且一次性全部调用总 cell 数量次 tableView:heightForRowAtIndexPath: 方法。
	*/

	return 60;
}

// 设置每一行显示的内容,每当有一个 cell 进入视野范围内就会调用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

	return cell;
}
  • 1.2 分段的头、脚标题 设置

// 设置分段的头标题高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

	return 40;
}

// 设置分段的脚标题高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {

	return 30;
}

// 设置分段的头标题估计高度
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section {

	return 40;
}

// 设置分段的脚标题估计高度
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section {

	return 30;
}

// 设置分段的头标题内容
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

	if (0 == section) {
		return @"1 Header";
	}
	else{
		return @"2 rHeader";
	}
}

// 设置分段的脚标题内容
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {

	if (0 == section) {
		return @"2 Footer";
	}
	else{
		return @"2 Footer";
	}
}

// 设置分段头标题视图,返回自定义的标题视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

	return myView;
}

// 设置分段脚标题视图,返回自定义的标题视图
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {

	return myView;
}
  • 1.3 分段索引条 设置

// 创建索引条
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {

	return array;
}

// 设置索引条偏移量,默认索引条与分段一一对应时,可以不写该方法
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
	/*
	点击索引条上字符串的时候 tableView 会跳转到对应的分段,是根据位置计算的,
	点击索引条上第几个,tableView 就会跳到第几段。

	如果索引条的前面加了个搜索小图标等,需要重写这个方法。
	*/
}
  • 1.4 表格点击 设置

// 表格选中点击响应事件,表格被选中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}

// 表格取消选中点击响应事件,表格被取消选中
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {

}

// 附属控件 button 点击响应事件
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {

// 如果系统自带的附属控件里有 button ,附属控件的点击事件会独立出来
}
  • 1.5 表格编辑 设置

// 表格删除、插入
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

// 表格删除或插入,默认为删除模式,写入该方法即表示允许删除。
}

// 设置编辑模式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {

// 删除、插入、多选删除,不设置默认时为删除
}

// 修改左滑删除按钮的内容
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {

	return @"删除";
}

// 设置左滑多按钮
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {

	// 按钮从右向左的顺序排列
	return @[action1, action0];
}

// 表格移动
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {

}

2、UIScrollViewDelegate 协议方法

  • 2.1 拖拽

// 将要开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {

}

// 将要结束拖拽
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {

}

// 已经结束拖拽,decelerate 松手后 是否有惯性滚动 0:没有,1:有
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {

}
  • 2.2 滚动

// 滚动过程中,只要滚动就会触发
- (void)scrollViewDidScroll:(UIScrollView *)scrollView { 

}

// 已经结束滚动,滚动动画停止时执行,代码改变时触发,也就是 setContentOffset 改变时
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {

}
  • 2.3 惯性滚动

// 将要开始惯性滚动
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {

}

// 已经结束惯性滚动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

}
  • 2.4 滚到顶端

// 设置点击状态栏时是否滚到顶端
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView {

	return YES;
}

// 已经滚到顶端,点击状态栏时调用
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {

}
  • 2.5 缩放

// 设置被缩放的空间,一个 scrollView 中只能有一个子控件被缩放,如果有很多个子控件缩放时会引起错乱
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {

	return [scrollView.subviews[0] viewWithTag:100];
}

// 将要开始缩放
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view {

}

// 已经结束缩放
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale {

}

// 缩放过程中,只要缩放就会触发
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {

}
posted @ 2018-08-05 12:50  CH520  阅读(385)  评论(0编辑  收藏  举报