UITableView的基本知识

 1、UITableView的概念:设计模式

UITableView 是iOS中最重要的控件,几乎全部的页面均可以用UITableView完成。数组

 

tableView的使用须要遵循代理和数据源,这也是一种很是棒的设计模式,数据源模式能够近似为代理模式。函数

tableview要引入2个代理UITableViewDelegate,UITableViewDataSource字体

 

2、UITableView的基本用法: ui

一、基本属性:
(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;rem

    

    //tableView里面的数据管理字符串

    tableView.dataSource = self;

    [self.view addSubview:tableView];

二、设置 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;

 

三、查看代理的方法:

// 设置对应的高度  

- (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;