求无心法师百度云盘链接2百度云资源

UITableView使用详解
在开发iphone的应用时基本上都要用到UITableView,这里讲解一下UITableView的使用方法及代理的调用情况
- (void)viewDidLoad
viewDidLoad];
//初始化数据
NSArray *array1_=@[@"张铁林",@"张国立",@"张国荣",@"张艺谋",@"张惠妹"];
*array2_=@[@"李小龙",@"李小路"];
*array3_=@[@"王刚"];
& self.myDic=@{@"老张家":array1_,
@"老李家":array2_,
@"老王家":array3_};
UITableView *myTableView_=[[UITableView alloc] initWithFrame:CGRectMake(0,
460) style:UITableViewStylePlain];
& myTableView_.delegate=self;
& myTableView_.dataSource=self;
//改变换行线颜色</
& myTableView_.separatorColor = [UIColor blueColor];
//设定Header的高度,
& myTableView_.sectionHeaderHeight=50;
//设定footer的高度,
& myTableView_.sectionFooterHeight=100;
//设定行高
& myTableView_.rowHeight=100;
//设定cell分行线的样式,默认为UITableViewCellSeparatorStyleSingleLine
[myTableView_ setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
//设定cell分行线颜色
[myTableView_ setSeparatorColor:[UIColor redColor]];
//编辑tableView
& myTableView_.editing=NO;
& [self.view addSubview:myTableView_];
//跳到指的row
or section
[myTableView_ scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:2
inSection:2]
atScrollPosition:UITableViewScrollPositionBottom
animated:NO];
//指定有多少个分区(Section),默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView
*)tableView {
[[self.myDic allKeys] count];
//每个section底部标题高度(实现这个代理方法后前面
sectionHeaderHeight 设定的高度无效)
-(CGFloat)tableView:(UITableView *)tableView
heightForHeaderInSection:(NSInteger)section{
//每个section头部标题高度(实现这个代理方法后前面
sectionFooterHeight 设定的高度无效)
-(CGFloat)tableView:(UITableView *)tableView
heightForFooterInSection:(NSInteger)section{
//每个section头部的标题-Header
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section{
[[self.myDic allKeys] objectAtIndex:section];
//每个section底部的标题-Footer
- (NSString *)tableView:(UITableView *)tableView
titleForFooterInSection:(NSInteger)section{
return nil;
//用以定制自定义的section头部视图-Header
-(UIView *)tableView:(UITableView *)tableView
viewForHeaderInSection:(NSInteger)section{
return nil;
//用以定制自定义的section底部视图-Footer
-(UIView *)tableView:(UITableView *)tableView
viewForFooterInSection:(NSInteger)section{
& UIImageView
*imageView_=[[UIImageView
alloc]initWithFrame:CGRectMake(0, 0,
320, 20)];
& imageView_.image=[UIImage imageNamed:@"1000.png"];
[imageView_ autorelease];
//指定每个分区中有多少行,默认为1
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section{
return [[self.myDic
objectForKey:[[self.myDic
allKeys]objectAtIndex:section]] count];
//改变行的高度(实现主个代理方法后
设定的高度无效)
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//绘制Cell
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *SimpleTableIdentifier
= @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:
& SimpleTableIdentifier];
& if (cell ==
& & cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:
SimpleTableIdentifier] autorelease];
& &//设定附加视图
setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
UITableViewCellAccessoryNone, & &
& // 没有标示
UITableViewCellAccessoryDisclosureIndicator,&
& // 下一层标示
UITableViewCellAccessoryDetailDisclosureButton, // 详情button
UITableViewCellAccessoryCheckmark &
& &//设定选中cell时的cell的背影颜色
&&cell.selectionStyle=UITableViewCellSelectionStyleBlue;
& & //选中时蓝色效果
cell.selectionStyle=UITableViewCellSelectionStyleN
//选中时没有颜色效果
cell.selectionStyle=UITableViewCellSelectionStyleG
&//选中时灰色效果
& & //自定义选中cell时的背景颜色
& & UIView *selectedView =
[[UIView alloc] initWithFrame:cell.contentView.frame];
& & selectedView.backgroundColor
= [UIColor orangeColor];
& & cell.selectedBackgroundView =
& & [selectedView
cell.selectionStyle=UITableViewCellAccessoryN //行不能被选中
& //这是设置没选中之前的背景颜色
cell.contentView.backgroundColor = [UIColor clearColor];
cell.imageView.image=[UIImage imageNamed:@"1001.jpg"];//未选cell时的图片
cell.imageView.highlightedImage=[UIImage imageNamed:@"1002.jpg"];//选中cell后的图片
& cell.textLabel.text=[[self.myDic objectForKey:[[self.myDic allKeys]objectAtIndex:indexPath.section]]objectAtIndex:indexPath.row];
-(NSInteger)tableView:(UITableView *)tableView
indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{
& NSUInteger
row = [indexPath row];
//选中Cell响应事件
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失
//得到当前选中的cell
& UITableViewCell *cell=[tableView
cellForRowAtIndexPath:indexPath];
& NSLog(@"cell=%@",cell);
//行将显示的时候调用,预加载行
-(void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath
*)indexPath
NSLog(@"将要显示的行是\n
cell=%@& \n indexpath=%@",cell,indexPath);
//选中之前执行,判断选中的行(阻止选中第一行)
-(NSIndexPath *)tableView:(UITableView *)tableView
willSelectRowAtIndexPath:(NSIndexPath *)indexPath
& NSUInteger
row = [indexPath row];
& if (row ==
return nil;
//编辑状态,点击删除时调用
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
//cell右边按钮格式为UITableViewCellAccessoryDetailDisclosureButton时,点击按扭时调用的方法
-(void)tableView:(UITableView *)tableView
accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
NSLog(@"当前点击的详情button
\n indexpath=%@",indexPath);
//右侧添加一个索引表
- (NSArray
*)sectionIndexTitlesForTableView:(UITableView *)tableView{
[self.myDic allKeys];
//划动cell是否出现del按钮
- (BOOL)tableView:(UITableView *)tableView
canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
//设定横向滑动时是否出现删除按扭,(阻止第一行出现)
-(UITableViewCellEditingStyle)tableView:(UITableView
*)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
(indexPath.row==0) {
& & return
UITableViewCellEditingStyleNone;
& & return
UITableViewCellEditingStyleDelete;
UITableViewCellEditingStyleDelete;
//自定义划动时delete按钮内容
- (NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath
*)indexPath{
return @"删除这行";
//开始移动row时执行
-(void)tableView:(UITableView *)tableView
moveRowAtIndexPath:(NSIndexPath
*)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath
& NSLog(@"sourceIndexPath=%@",sourceIndexPath);
& NSLog(@"sourceIndexPath=%@",destinationIndexPath);
//滑动可以编辑时执行
-(void)tableView:(UITableView *)tableView
willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
NSLog(@"willBeginEditingRowAtIndexPath");
//将取消选中时执行,
也就是上次先中的行
-(NSIndexPath *)tableView:(UITableView *)tableView
willDeselectRowAtIndexPath:(NSIndexPath *)indexPath
NSLog(@"上次选中的行是&
\n indexpath=%@",indexPath);
//让行可以移动
-(BOOL)tableView:(UITableView *)tableView
canMoveRowAtIndexPath:(NSIndexPath *)indexPath
return NO;
//移动row时执行
-(NSIndexPath *)tableView:(UITableView *)tableView
targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath
toProposedIndexPath:(NSIndexPath
*)proposedDestinationIndexPath
NSLog(@"targetIndexPathForMoveFromRowAtIndexPath");
& //用于限制只在当前section下面才可以移动
& if(sourceIndexPath.section !=
proposedDestinationIndexPath.section){
sourceIndexP
proposedDestinationIndexP
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。記錄一下,tableHeaderView會隨著底下cell移動到消失。
viewForHeaderInSection會一直固定在那裡。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:50725次
积分:1226
积分:1226
排名:千里之外
原创:71篇
转载:18篇
评论:15条
(1)(2)(3)(4)(1)(3)(2)(3)(3)(3)(5)(7)(7)(6)(8)(1)(3)(1)(4)(4)(4)(2)(4)(2)(7)[ios]UITableView: 如何获取内 viewForHeaderInSection heightForHeaderInSection
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.
我创造了一个自定义节页眉视图为我 UITableView 。
但如何获取 tableHeight 我 viewForHeaderInSection 方法吗?我目前正在做以下和 tableHeight 保存的值 0。
-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
if (includeButton)
return 55;
return 30;
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
CGFloat tableHeight = [tableView headerViewForSection:section].frame.size.
解决方法 1:
这两种方法是在同一视图控制器中。只是调用您的实现并传递的表视图和部分,就像这样:
CGFloat tableHeight = [self tableView:tableView heightForHeaderInSection:section];本帖子已过去太久远了,不再提供回复功能。

我要回帖

更多关于 无心法师百度云盘链接 的文章

 

随机推荐