UITableView的基本知识

来源:互联网 发布:武汉网络推广 编辑:程序博客网 时间:2024/06/02 00:14

一、UITableView的概念:

UITableView 是iOS中最重要的控件,几乎所有的页面都可以用UITableView完成。

tableView的使用需要遵循代理和数据源,这也是一种非常棒的设计模式,数据源模式可以近似为代理模式。

tableview要引入2个代理UITableViewDelegate,UITableViewDataSource

二、UITableView的基本用法:

1、基本属性:

(1)设置tableview的类型

UITableViewStylePlain            基本类型,分区头标题会悬浮

UITableViewStyleGrouped    分组的类型,分区头标题不会悬浮

//初始化:

UITableView  *tableView =  [[UITableView alloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)  style:UITableViewStylePlain];

(2)设置背景:

tableView.backgroundColor = [UIColor redColor];

(3)设置分割线:

类型:tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

颜色:tableView.separatorColor = [UIColor blackColor];

位置:tableView. separatorInset = UIEdgeInsetsMake(0, 20, 0, 20);

(4)数据源和代理:

//主要管视图内容

tableView.delegate = self;

//tableView里面的数据管理

tableView.dataSource = self;

[self.view addSubview:tableView];

2、设置 UITableViewDelegate的方法:

//每个section有多少rows:(必须实现的方法)

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

//返回指定的 row 的高度:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

//返回指定的 section的header view 的高度:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

//返回指定的 section的footer view 的高度:

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;

//返回指定的row 的cell:(必须实现的方法)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath;

eg:

static NSString* cellname = @"cellname";

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

//从tableveiw的复用池,是不是有可以复用的cell

//dequeueReusableCellWithIdentifier

TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellname];

//初始化cell

if (cell == nil) {

//xib文件的初始化

cell = [[[NSBundle mainBundle]loadNibNamed:@"TableViewCell" owner:self options:nil]lastObject];

}

//没有点击效果::

cell.selectionStyle = UITableViewCellSelectionStyleNone;

cell.label.text = self.arr_tv[indexPath.row];

//自定义字体的uifont

cell.label.font = [UIFont fontWithName:self.arr_tv[indexPath.row] size:13];

//    NSLog(@"cellForRowAtIndexPath-%@",cell);

return cell;

}

//  取消选中的效果::

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

{

[tableView deselectRowAtIndexPath:indexPath animated:YES];// 取消选中

//其他代码

}

//返回指定的 section 的header的高度;

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

// 设置section的数量,默认为1

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

//section由两部分组成,header和footer,分别设置各自需要展示的字符串,也可以自定义显示的样式,需要用到其他的方法

- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;

- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;

eg:- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

switch (section) {

case 0:

return [titleArray objectAtIndex:section];//提取标题数组的元素用来显示标题

case 1:

return [titleArray objectAtIndex:section];//提取标题数组的元素用来显示标题

default:

return @"Unknown";

}

}

// 哪些row可以被编辑

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;

// Moving/reordering  移动

// 哪些row可以被移动

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;

//如何设置tableview 可以被编辑

[TableView setEditing:YES animated:YES];

如果要退出编辑模式,肯定就是设置为NO

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

返回当前cell  要执行的是哪种编辑,下面的代码是 返回 删除  模式

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

return UITableViewCellEditingStyleDelete;

}

//        -(void) tableView:(UITableView *)aTableView

commitEditingStyle:(UITableViewCellEditingStyle) editingStyle

forRowAtIndexPath:(NSIndexPath *)indexPath

通知告诉用户编辑了 哪个cell,对应上面的代码,我们在这个函数里面执行删除cell的操作。

-(void) tableView:(UITableView *)aTableView

commitEditingStyle:(UITableViewCellEditingStyle) editingStyle

forRowAtIndexPath:(NSIndexPath *)indexPath{

[chatArray  removeObjectAtIndex:indexPath.row];

[chatTableView  reloadData];

}

//如何获得 某一行的CELL对象

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;

3、查看代理的方法:

// 设置对应的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;

// 自定义header和footer

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;

// 设置编辑的样式

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



    文/小铭_同学(简书作者)
    原文链接:http://www.jianshu.com/p/9c8ba9d21369
    著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
    0 0
    原创粉丝点击