UITableView

本文详细介绍了UITableView的基本概念、使用方法及编辑操作等内容。UITableView继承自UIScrollView,提供了Plain和Grouped两种样式,并可通过UITableViewDataSource和UITableViewDelegate来定制数据展示和交互。文章还讲解了NSIndexPath的作用及其属性,以及如何设置UITableView的样式和属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UITableView继承自UIScrollView,可以表现为Plain和Grouped两种风格

UITableView有两个Delegate分别为:dataSource和delegate。

  dataSource是UITableViewDataSource类型,主要为UITableView提供显示用的数据(UITableViewCell),指定UITableViewCell支持的编辑操作类型(insert,delete和reordering),并根据用户的操作进行相应的数据更新操作,如果数据没有更具操作进行正确的更新,可能会导致显示异常,甚至crush。

  delegate是UITableViewDelegate类型,主要提供一些可选的方法,用来控制tableView的选择、指定section的头和尾的显示以及协助完成cell的删除和排序等功能。

  提到UITableView,就必须的说一说NSIndexPath。UITableView声明了一个NSIndexPath的类别,主要用来标识当前cell的在tableView中的位置,该类别有section和row两个属性,前者标识当前cell处于第几个section中,后者代表在该section中的第几行。

  UITableView只能有一列数据(cell),且只支持纵向滑动,当创建好的tablView第一次显示的时候,我们需要调用其reloadData方法,强制刷新一次,从而使tableView的数据更新到最新状态。

系统提供的UITableView也包含了四种风格的布局,分别是:

typedef enum {
    UITableViewCellStyleDefault,
    UITableViewCellStyleValue1,
    UITableViewCellStyleValue2,
    UITableViewCellStyleSubtitle
} UITableViewCellStyle;

celldeleteinsert操作大部分流程都是一样的,当进入编辑模式的时候具体的显示是delete还是insert取决与该celleditingStyle的值,editStyle的定义如下:

typedef enum {
    UITableViewCellEditingStyleNone,
    UITableViewCellEditingStyleDelete,
    UITableViewCellEditingStyleInsert
} UITableViewCellEditingStyle;

分割线

  我们可以通过设置tableViewseparatorStyle属性来设置有无分割线以及分割线的风格,其中style定义如下:

typedef enum {
    UITableViewCellSeparatorStyleNone,
    UITableViewCellSeparatorStyleSingleLine,
    UITableViewCellSeparatorStyleSingleLineEtched
} UITableViewCellSeparatorStyle;


<UITableViewDataSource,UITableViewDelegate>里的方法: tableView处理步骤 
#pragma mark 1.有多少组 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
#pragma mark 2.第section组头部控件有多高 
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
#pragma mark 3.第section组有多少行 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section #pragma mark 
4.indexPath这行的cell有多高 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
#pragma mark 5.indexPath这行的cell长什么样子 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
#pragma mark 6.第section组头部显示什么控件 
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
 
//每当有一个cell进入视野屏幕就会调用,所以在这个方法内部就需要优化。 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
if(cell==nil){   
//在这里面做创建的工作。循环优化。防止刷新cell进入屏幕的时候重复的创建 
}  
} 
 
//当调用reloadData的时候,会重新刷新调用数据源内所有方法,其他事情都不会做呀  
[self reloadData]
//这个方法只有在一开始有多少条数据才会算多少个高度,这个方法只会调用一次,但是每次reloadData的时候也会调用  
//而且会一次性算出所有cell的高度,比如有100条数据,一次性调用100次 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath     
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView //右侧索引 
 
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath //行点击事件  
NSIndexPath *path = [self.tableView indexPathForSelectedRow]; //获得被选中的indexPath可以得到section,row

self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForSelectedRows] withRowAnimation:UITableViewRowAnimationNone]; 

//刷新table指定行的数据           

   [self.tableView reloadData]; //刷新table所有行的数据 
  
UITableView常用属性: 

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

// 初始化表格 

    分隔线属性  

tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; 

//UITableViewCellSeparatorStyleNone; 

[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; //取消分隔线     

tableView.separatorColor = [UIColor lightGrayColor];      

    条目多选 
tableView.allowsMultipleSelection = YES;

// 设置标题行高 
[_tableView setSectionHeaderHeight:kHeaderHeight];     

[_tableView setSectionFooterHeight:0]; 
 // 设置表格行高 
 [_tableView setRowHeight:50];  
//设置背景色 
self.tableView.backgroundView  优先级高,如果要设置backgroundColor的时候要先把view设置为nil 

self.tableView.backgroundColor  
//在tableView的头部或者尾部添加view,footerView宽度是不用设置的        

xxxView.bounds = CGRectMake(0,0,0,height);  
self.tableView.tableFooterView =xxxView; 
self.tableView.tableHeaderView =xxxView; 
 
UIButton *bt = (UIButton*)[self.contentView viewWithTag:i+100]; 
  
增加tableview滚动区域 
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, xx, 0);


#pragma mark - 编辑操作
//1.设置是否 可编辑
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    //设置tableView是否 可编辑
    [_tableView setEditing:editing animated:animated];
}

//删除
//2.某一行是否可编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (indexPath.row % 2 == 0) {
//        return NO;
//    }
    return YES;
}

//3.设置编辑风格
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

//4.提交编辑操作
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    //删除数据
    //获取对应的key值
    NSString *key = _allKeys[indexPath.section];
    //获取对应的分组
    NSMutableArray *group = [_classDic valueForKey:key];
    if (group.count == 1) {
        //删除分组
        [_classDic removeObjectForKey:key];
        //删除对应的key
        [_allKeys removeObject:key];
        //再删除 UI界面的分区
        NSIndexSet *set = [[NSIndexSet alloc] initWithIndex:indexPath.section];
        
        [tableView deleteSections:set withRowAnimation:(UITableViewRowAnimationRight)];
              
    }else
    {
    
    //删除对应联系人
    [group removeObjectAtIndex:indexPath.row];
    
    //UI界面的删除
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
    }
}

//移动
//2.设置某一行是否可以移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

//3.设置 是否可以跨区域
-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    //通过分区 判定
    if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
        //如果在一个分区 就返回目标位置
        return proposedDestinationIndexPath;
    }
    //如果不在一个分区 返回原来的位置
    return sourceIndexPath;
}

//4.提交移动操作
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    //获取对应key
    NSString *key = _allKeys[sourceIndexPath.section];
   
    //获取对应分组
    NSMutableArray *group = [_classDic valueForKey:key];
    
    //获取对应联系人
    Contact *contact = [group[sourceIndexPath.row]retain];  
    [group removeObjectAtIndex:sourceIndexPath.row];
    
    //再插入目标位置
    [group insertObject:contact atIndex:destinationIndexPath.row];
    [contact release];
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值