UITableView使用详解

*在开发iphone的应用时基本上都要用到UITableView,这里讲解一下UITableView的使用方法及代理的调用情况

转自:(http://blog.sina.com.cn/s/blog_9693f61a01016lv5.html)

- (void)viewDidLoad

{

[superviewDidLoad];

//初始化数据

NSArray*array1_=@[@"张铁林",@"张国立",@"张国荣",@"张艺谋",@"张惠妹"];

NSArray*array2_=@[@"李小龙",@"李小路"];

NSArray*array3_=@[@"王刚"];

self.myDic=@{@"老张家":array1_,@"老李家":array2_,@"老王家":array3_};

UITableView*myTableView_=[[UITableViewalloc]initWithFrame:CGRectMake(0,0,320,460)style:UITableViewStylePlain];

myTableView_.delegate=self;

myTableView_.dataSource=self;

//改变换行线颜色lyttzx.com

myTableView_.separatorColor= [UIColorblueColor];

//设定Header的高度,

myTableView_.sectionHeaderHeight=50;

//设定footer的高度,

myTableView_.sectionFooterHeight=100;

//设定行高

myTableView_.rowHeight=100;

//设定cell分行线的样式,默认为UITableViewCellSeparatorStyleSingleLine

[myTableView_setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];

//设定cell分行线颜色

[myTableView_setSeparatorColor:[UIColorredColor]];

//编辑tableView

myTableView_.editing=NO;

[self.viewaddSubview:myTableView_];

//跳到指的rowor section

[myTableView_scrollToRowAtIndexPath:[NSIndexPathindexPathForRow:2inSection:2]

atScrollPosition:UITableViewScrollPositionBottomanimated:NO];

}

//指定有多少个分区(Section),默认为1

- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView {

return[[self.myDicallKeys]count];

}

//每个section底部标题高度(实现这个代理方法后前面sectionHeaderHeight设定的高度无效)

-(CGFloat)tableView:(UITableView*)tableViewheightForHeaderInSection:(NSInteger)section{

return20;

}

//每个section头部标题高度(实现这个代理方法后前面sectionFooterHeight设定的高度无效)

-(CGFloat)tableView:(UITableView*)tableViewheightForFooterInSection:(NSInteger)section{

return20;

}

//每个section头部的标题-Header

- (NSString*)tableView:(UITableView*)tableViewtitleForHeaderInSection:(NSInteger)section{

return[[self.myDicallKeys]objectAtIndex:section];

}

//每个section底部的标题-Footer

- (NSString*)tableView:(UITableView*)tableViewtitleForFooterInSection:(NSInteger)section{

return nil;

}

//用以定制自定义的section头部视图-Header

-(UIView*)tableView:(UITableView*)tableViewviewForHeaderInSection:(NSInteger)section{

return nil;

}

//用以定制自定义的section底部视图-Footer

-(UIView*)tableView:(UITableView*)tableViewviewForFooterInSection:(NSInteger)section{

UIImageView*imageView_=[[UIImageViewalloc]initWithFrame:CGRectMake(0,0,320,20)];

imageView_.image=[UIImageimageNamed:@"1000.png"];

return[imageView_autorelease];

}

//指定每个分区中有多少行,默认为1

- (NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{

return[[self.myDicobjectForKey:[[self.myDicallKeys]objectAtIndex:section]]count];

}

//改变行的高度(实现主个代理方法后rowHeight设定的高度无效)

- (CGFloat)tableView:(UITableView*)tableViewheightForRowAtIndexPath:(NSIndexPath*)indexPath{

return100;

}

//绘制Cell

-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath {

staticNSString*SimpleTableIdentifier=@"SimpleTableIdentifier";

UITableViewCell*cell = [tableViewdequeueReusableCellWithIdentifier:

SimpleTableIdentifier];

if(cell ==nil) {

cell = [[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefault

reuseIdentifier:SimpleTableIdentifier]autorelease];

//设定附加视图

[cellsetAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];

//UITableViewCellAccessoryNone,//没有标示

//UITableViewCellAccessoryDisclosureIndicator,//下一层标示

//UITableViewCellAccessoryDetailDisclosureButton, //详情button

//UITableViewCellAccessoryCheckmark//勾选标记

//设定选中cell时的cell的背影颜色

cell.selectionStyle=UITableViewCellSelectionStyleBlue;//选中时蓝色效果

//cell.selectionStyle=UITableViewCellSelectionStyleNone;//选中时没有颜色效果

//cell.selectionStyle=UITableViewCellSelectionStyleGray;//选中时灰色效果

//

////自定义选中cell时的背景颜色

//UIView *selectedView =[[UIView alloc] initWithFrame:cell.contentView.frame];

//selectedView.backgroundColor= [UIColor orangeColor];

//cell.selectedBackgroundView =selectedView;

//[selectedViewrelease];

//cell.selectionStyle=UITableViewCellAccessoryNone; //行不能被选中

}

//这是设置没选中之前的背景颜色

cell.contentView.backgroundColor= [UIColorclearColor];

cell.imageView.image=[UIImageimageNamed:@"1001.jpg"];//未选cell时的图片

cell.imageView.highlightedImage=[UIImageimageNamed:@"1002.jpg"];//选中cell后的图片

cell.textLabel.text=[[self.myDicobjectForKey:[[self.myDicallKeys]objectAtIndex:indexPath.section]]objectAtIndex:indexPath.row];

returncell;

}

//行缩进

-(NSInteger)tableView:(UITableView*)tableViewindentationLevelForRowAtIndexPath:(NSIndexPath*)indexPath{

NSUIntegerrow = [indexPathrow];

returnrow;

}

//选中Cell响应事件

- (void)tableView:(UITableView*)tableViewdidSelectRowAtIndexPath:(NSIndexPath*)indexPath{

[tableViewdeselectRowAtIndexPath:indexPathanimated:YES];//选中后的反显颜色即刻消失

//得到当前选中的cell

UITableViewCell*cell=[tableViewcellForRowAtIndexPath:indexPath];

NSLog(@"cell=%@",cell);

}

//行将显示的时候调用,预加载行

-(void)tableView:(UITableView*)tableViewwillDisplayCell:(UITableViewCell*)cellforRowAtIndexPath:(NSIndexPath*)indexPath

{

NSLog(@"将要显示的行是\ncell=%@\n indexpath=%@",cell,indexPath);

}

//选中之前执行,判断选中的行(阻止选中第一行)

-(NSIndexPath*)tableView:(UITableView*)tableViewwillSelectRowAtIndexPath:(NSIndexPath*)indexPath

{

NSUIntegerrow = [indexPathrow];

if(row ==0)

returnnil;

returnindexPath;

}

//编辑状态,点击删除时调用

- (void)tableView:(UITableView*)tableViewcommitEditingStyle:(UITableViewCellEditingStyle)editingStyle

forRowAtIndexPath:(NSIndexPath*)indexPath

{

}

//cell右边按钮格式为UITableViewCellAccessoryDetailDisclosureButton时,点击按扭时调用的方法

-(void)tableView:(UITableView*)tableViewaccessoryButtonTappedForRowWithIndexPath:(NSIndexPath*)indexPath{

NSLog(@"当前点击的详情button\n indexpath=%@",indexPath);

}

//右侧添加一个索引表

- (NSArray*)sectionIndexTitlesForTableView:(UITableView*)tableView{

return[self.myDicallKeys];

}

//划动cell是否出现del按钮

- (BOOL)tableView:(UITableView*)tableViewcanEditRowAtIndexPath:(NSIndexPath*)indexPath {

return YES;

}

//设定横向滑动时是否出现删除按扭,(阻止第一行出现)

-(UITableViewCellEditingStyle)tableView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath

{

if(indexPath.row==0) {

returnUITableViewCellEditingStyleNone;

}

else{

returnUITableViewCellEditingStyleDelete;

}

returnUITableViewCellEditingStyleDelete;

}

//自定义划动时delete按钮内容

- (NSString*)tableView:(UITableView*)tableView

titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath*)indexPath{

return@"删除这行";

}

//开始移动row时执行

-(void)tableView:(UITableView*)tableViewmoveRowAtIndexPath:(NSIndexPath*)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath

{

NSLog(@"sourceIndexPath=%@",sourceIndexPath);

NSLog(@"sourceIndexPath=%@",destinationIndexPath);

}

//滑动可以编辑时执行

-(void)tableView:(UITableView*)tableViewwillBeginEditingRowAtIndexPath:(NSIndexPath*)indexPath

{

NSLog(@"willBeginEditingRowAtIndexPath");

}

//将取消选中时执行,也就是上次先中的行

-(NSIndexPath*)tableView:(UITableView*)tableViewwillDeselectRowAtIndexPath:(NSIndexPath*)indexPath

{

NSLog(@"上次选中的行是\n indexpath=%@",indexPath);

returnindexPath;

}

//让行可以移动

-(BOOL)tableView:(UITableView*)tableViewcanMoveRowAtIndexPath:(NSIndexPath*)indexPath

{

return NO;

}

//移动row时执行

-(NSIndexPath*)tableView:(UITableView*)tableViewtargetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath*)sourceIndexPathtoProposedIndexPath:(NSIndexPath*)proposedDestinationIndexPath

{

NSLog(@"targetIndexPathForMoveFromRowAtIndexPath");

//用于限制只在当前section下面才可以移动

if(sourceIndexPath.section!=proposedDestinationIndexPath.section){

returnsourceIndexPath;

}

returnproposedDestinationIndexPath;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335

推荐阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 8,908评论 3 38
  • 版权声明:未经本人允许,禁止转载. 1. TableView初始化 1.UITableView有两种风格:UITa...
    萧雪痕阅读 2,882评论 2 10
  • 转自:“http://www.open-open.com/lib/view/open1430008922468.h...
    深蓝_S阅读 1,280评论 0 0
  • NSArray*array1_=@[@"张铁林",@"张国立",@"张国荣",@"张艺谋",@"张惠妹"]; NS...
    Sherry宇阅读 439评论 0 2
  • 今天早上在群里说最近脑子有点乱,空空洞洞的,不知道做什么,群里的前辈几个出来支持我鼓励我,蔡杰还推荐了很多书让我跳...
    誼君阅读 161评论 0 0