20-01 UITableView 表视图

一、基本介绍

在众多移动应⽤用中,能看到各式各样的表格数据 。

在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UITableView继承自UIScrollView,因此支持垂直滚动,⽽且性能极佳 。

UITableview有分组和不分组两种样式,可以在storyboard或者是用代码设置。


二、数据展示

UITableView需要⼀一个数据源(dataSource)来显示数据
UITableView会向数据源查询一共有多少行数据以及每⼀行显示什么数据等

没有设置数据源的UITableView只是个空壳

凡是遵守UITableViewDataSource协议的OC对象,都可以是UITableView的数据源 


展示数据的过程:

(1)调用数据源的下面⽅法得知⼀一共有多少组数据
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

(2)调用数据源的下面⽅法得知每一组有多少行数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

(3)调⽤数据源的下⾯⽅法得知每⼀⾏显示什么内容

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


三、常用属性设置
1.属性
//-------------------------------------------设置表视图属性---------------------------------
// 设置分割线样式
tableview.separatorStyle = UITableViewCellSeparatorStyleNone;

// 设置分割线颜色
tableview.separatorColor = [UIColor redColor];

/**取消点击单元格选中 带动画*/
[tableView deselectRowAtIndexPath:indexPath animated:YES];

// 设置tableview的头部和尾部视图   可添加 UIView
tableview.tableHeaderView = [[UIImageView alloc] initWithImage:@“”];;
tableview.tableFooterView = [[UISwitch alloc] init];

 /**tablView自定义头视图*/
_headView = [[[NSBundle mainBundle] loadNibNamed:@"HeadTabView" owner:self options:nil] lastObject];
_tablView.tableHeaderView = _headView;

// 设置tableview背景视图          可添加 UIView
tableview.backgrundView = [[UIImageView alloc] initWithImage:@“”];

//当前屏幕上显示的单元格
NSArray *visibleArray = [_myTableView visibleCells];

//隐藏滑动条
_tabView.showsVerticalScrollIndicator = NO;
//-------------------------------------------设置分组属性---------------------------------
// 设置section分组的头部和尾部高度
tableview.sectionHeaderHeight = 10;
tableview.sectionFooterHeight = 10;

// 修改数据之后要刷新数据
[self.tableview reloadData]

//刷新一行 刷新一行单元格
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

//-------------------------------------------设置cell属性---------------------------------
//创建NSIndexPath//滑动到指定位置
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:8 inSection:0];
//点击按钮滚动到第8行
[_myTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

// 设置cell高度 默认44
1)self.tableView.rowHeight = 60;这适用于当每一行的cell高度一致的时候,使用属性设置cell的高度。
2)在storyboard中设置,适用于高度一致

3)当每一行的cell的高度不一致的时候就使用代理方法设置cell的高度
//delegate协议方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    if (1 == indexPath.row) {
       
        return 180;  
    }  
    return 44;
}

// 设置cell背景色 
cell.backgroundColor = [UIColor blueColor];
cell.contentView.backgroundColor = [UIColor grayColor];

// 设置cell默认背景视图
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"buttondelete"]]; cell.backgroundView = iv;

// 设置cell选中状态的背景
UIView *view2 = [[UIView alloc] init];
view2.backgroundColor = [UIColor purpleColor];
cell.selectedBackgroundView = view2;
  
//设置选中表格效果
cell.selectionStyle = UITableViewCellSelectionStyleBlue;


//图像
cell.imageView.image = [UIImage imageNamed:lol.icon];



// 辅助视图设置cell右边指示类型
cell. accessoryType = UITableViewCellAccessoryDisclosureIndicator ;
如图:

// 自定义辅助视图
cell. accessoryView = [[ UISwitch alloc ] init ];

//显示描述 设置副标题
cell = [[ UITableViewCell alloc ] initWithStyle : UITableViewCellStyleSubtitle reuseIdentifier :identify];
//显示描述信息一定要把cell对象类型设置为UITableViewCellStyleSubtitle
cell.detailTextLabel.text = lol.intro;


3.UItableView delegate 代理方法

1.//选中某一行时
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat i = indexPath.row;
   
    scrollView.contentOffset = CGPointMake(i*375, 0);
}

2.设置组的头和尾标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *s = [NSString stringWithFormat:@"第%ld组", section+1];
   
    return s;
}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

3.自定义section头视图 一定要在代理方法中设置行高  此方法优先级比titleForHeaderInSection
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
   
    UIView *superView=[[UIView alloc]initWithFrame:CGRectZero];
   
    UILabel *textLabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 0, 100, 30)];
   
    textLabel.text=@"头视图";
   
    [superView addSubview:textLabel];
   
    return superView; 
}

4.设置分组头视图
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [_provinces objectAtIndex:section];
}

5.设置右侧分组
- ( NSArray *)sectionIndexTitlesForTableView:( UITableView *)tableView
{
   
return   _provinces ;
}

6.修改某一行cell的高度  可设置单元格高度自适应
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    if (1 == indexPath.row) {
       
        return 180;  
    }  
    return 44;
}

7.修改某一组的头和尾高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;

8.选中单元格调用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath



9.删除单元格数据时调用
/**
 *  如果实现了这个方法,就自动实现了滑动删除的功能
 *  点击了删除按钮就会调用
 *  提交了一个编辑操作就会调用(操作:删除\添加)
 *  @param editingStyle 编辑的行为
 *  @param indexPath    操作的行号
 */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) { // 提交的是删除操作
        // 1.删除模型数据
        [self.contacts removeObjectAtIndex:indexPath.row];
       
        // 2.刷新表格
        // 局部刷新某些行(使用前提:模型数据的行数不变)
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
       
        // 3.归档
        [NSKeyedArchiver archiveRootObject:self.contacts toFile:MJContactsFilepath];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // 1.修改模型数据
        MJContact *contact = [[MJContact alloc] init];
        contact.name = @"jack";
        contact.phone = @"10086";
        [self.contacts insertObject:contact atIndex:indexPath.row + 1];
       
        // 2.刷新表格
        NSIndexPath *nextPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:0];
        [self.tableView insertRowsAtIndexPaths:@[nextPath] withRowAnimation:UITableViewRowAnimationBottom];
//        [self.tableView reloadData];
    }
}

10.移动单元格
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {

    //改变数据
    [_data exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
}


.设置处于编辑模式的时候,编辑的类型:添加,删除,none
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {

//    UITableViewCellEditingStyleNone,
//    UITableViewCellEditingStyleDelete,
//    UITableViewCellEditingStyleInsert

    return UITableViewCellEditingStyleDelete;   //默认就是delete
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值