UITbableView基础学习记录

UITbableView笔记记录


  • 基础

UITableView-------表视图--继承UIScrollView并遵守NSCoding协议属性 frame-------------设置控件的位置和大小 backgroundColor--------设置控件的颜色 style--------获取表视图的样式 dataSource---------设置UITableViewDataSource的代理 delegate---------设置UITableViewDelegate代理 sectionHeaderHeight------设置组表视图的头标签高度 sectionFooterHeight--------设置级表视图的尾标签高度 backgroundView----------设置背景视图,只能写入 editing----------是否允许编辑,默认是NO allowsSelection----------在非编辑下,行是否可以选中,默认为YES allowsSelectionDuringEditing----------控制某一行时,是否可以编辑,默认为NO allowsMultipleSelection--------是否可以选择多行,默认为NO allowsMutableSelectionDuringEditing----------在选择多行的情况下,是否可以编辑,默认为NO sectionIndexMinimumDisplayRowCount-------------显示某个组索引列表在右边当行数达到这个值,默认是NSInteger的最大值 sectionIndexColor------------选择某个部分的某行改变这一行上文本的颜色 sectionIndexTrackingBackgroundColor--------设置选中某个部分的背景颜色 separatorStyle----------设置单元格分隔线的样式 separatorColor---------设置选中单元格分隔线的颜色 tableHeaderView---------设置组表的头标签视图 tableFooterView----------设置组表的尾标签视图 UITableView类目属性 section--------获取当前在哪个组内 row------------获取当前单元格是第几行 UITableView有两种风格:UITableViewStylePlain(普通样式)和UITableViewStyleGrouped(分组样式)

  • UITableView方法:
    初始化方法: initWithFrame:-----------设置表的大小和位置 initWithFrame:style---------设置表的大小,位置和样式(组,单一) setEditing:----------表格进入编辑状态,无动画 setEditing: animated:---------表格进入编辑状态,有动画 reloadData---------------刷新整个表视图 reloadSectionIndexTitles--------刷新索引栏 numberOfSections-----------获取当前所有的组 numberOfRowsInSection:---------获取某个组有多少行 rectForSection:----------获取某个组的位置和大小 rectForHeaderInSection:---------获取某个组的头标签的位置和大小 rectForFooterInSection:-----------获取某个组的尾标签的位置和大小 rectForRowAtIndex:-----------获取某一行的位置和大小 indexPathForRowAtPoint-------------点击某一个点,判断是在哪一行上的信息。 indexPathForCell:------------获取单元格的信息 indexPathsForRowsInRect:---------在某个区域里会返回多个单元格信息 cellForRowAtIndexPath:-------------通过单元格路径得到单元格 visibleCells-----------返回所有可见的单元格 indexPathsForVisibleRows--------返回所有可见行的路径 headerViewForSection:--------设置头标签的视图 footerViewForSection;----------设置尾标签的视图 beginUpdates--------只添加或删除才会更新行数 endUpdates---------添加或删除后会调用添加或删除方法时才会更新 insertSections:withRowAnimation:-----------插入一个或多个组,并使用动画 insertRowsIndexPaths:withRowAnimation:-------插入一个或多个单元格,并使用动画 deleteSections:withRowAnimation:--------删除一个或多个组,并使用动画 deleteRowIndexPaths:withRowAnimation:--------删除一个或多个单元格,并使用动画 reloadSections:withRowAnimation:---------更新一个或多个组,并使用动画 reloadRowIndexPaths:withRowAnimation:-------------更新一个或多个单元格,并使用动画 moveSection:toSection:-------------移动某个组到目标组位置 moveRowAtIndexPath:toIndexPath:-----------移动个某个单元格到目标单元格位置 indexPathsForSelectedRow----------返回选择的一个单元格的路径 indexPathsForSelectedRows---------返回选择的所有的单元格的路径 selectRowAtIndexPath:animation:scrollPosition---------设置选中某个区域内的单元格 deselectRowAtIndexPath:animation:----------取消选中的单元格 重用机制 dequeueReusableCellWithIdentifier:---------获取重用队列里的单元格

UITableViewDataSource代理方法:
numberOfSectionsInTableView:------------设置表格的组数 tableView:numberOfRowInSection:----------设置每个组有多少行 tableView:cellForRowAtIndexPath:---------设置单元格显示的内容 tableView:titleForHeaderInSection:---------设置组表的头标签视图 tableView:titleForFooterInSection:-----------设置组表的尾标签视图 tableView:canEditRowAtIndexPath:---------设置单元格是否可以编辑 tableView:canMoveRowAtIndexPath:--------设置单元格是否可以移动 tableView:sectionIndexTitleForTableView:atIndex:-------设置指定组的表的头标签文本 tableView:commitEditingStyle:forRowAtIndexPath:----------编辑单元格(添加,删除) tableView:moveRowAtIndexPath:toIndexPath-------单元格移动

UITableViewDelegate代理方法:
tableView: willDisplayCell: forRowAtIndexPath:-----------设置当前的单元格 tableView: heightForRowAtIndexPath:-----------设置每行的高度 tableView:tableView heightForHeaderInSection:-----------设置组表的头标签高度 tableView:tableView heightForFooterInSection:-------------设置组表的尾标签高度 tableView: viewForHeaderInSection:----------自定义组表的头标签视图 tableView: viewForFooterInSection: ----------自定义组表的尾标签视图 tableView: accessoryButtonTappedForRowWithIndexPath:-----------设置某个单元格上的右指向按钮的响应方法 tableView: willSelectRowAtIndexPath:-----------获取将要选择的单元格的路径 tableView: didSelectRowAtIndexPath:-----------获取选中的单元格的响应事件 tableView: tableView willDeselectRowAtIndexPath:------------获取将要未选中的单元格的路径 tableView: didDeselectRowAtIndexPath:-----------获取未选中的单元格响应事件
原文链接

直接上代码

我们需要先创建一个UITbableView对象然后加载在当前的显示的视图上:
当我们创建tableView可以使用懒加载的方式(创建并设置代理):

-(UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
_tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
}
return _tableView;
}


把我们创建的tableView加到self.view上

-(void)viewDidLoad {
[super viewDidLoad];
self.edgesForExtendedLayout = UIRectEdgeNone;
[self.view addSubview:self.tableView];
}

然后实现代理方法:

下面这两个是必须要实现的代理方法:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
// 设置每行的内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellID = @"cellid";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.textLabel.text = [NSString stringWithFormat:@"第%ld行",indexPath.row];
return cell;
}


以下方法可选择性实现:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}

返回的组头View

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
view.backgroundColor = [UIColor redColor];
return view;
}

返回组头高度

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 30;
}

返回的组尾View

-( UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
}

返回组尾高度

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
}

界面.png

返回每组行数:

-(NSInteger)tableView:(UITableView )tableView numberOfRowsInSection:(NSInteger)section{
NSLog(@"计算每组(组%i)行数",section);
KCContactGroup group1=_contacts[section];
return group1.contacts.count;
}

返回每组头标题名称:

-(NSString )tableView:(UITableView )tableView titleForHeaderInSection:(NSInteger)section{
}

返回每组尾部说明:

-(NSString )tableView:(UITableView )tableView titleForFooterInSection:(NSInteger)section{
}

右侧添加一个索引表

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

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

推荐阅读更多精彩内容