iOS-------UITableView

UITableView是iOS中显示多条数据的控件,UITableView的功能很强大,做笔记记录一下

属性:

(1)style:tableview的风格,分为两种:
   plain—默认的格式,一般的tableview
   group—这种风格的tableview,如果tableview的section有headview的话,tableview的section header会随着tableview一起滚动,代码演示
self.tableview.stylt=UITableViewStyleGrouped;
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 220;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return self.headerView;
    }
    return nil;
}
(2) estimatedRowHeight:在iOS 8之前,如果想在表视图中展示可变高度的动态内容时,你需要手动计算行高,而Self Sizing Cells为展示动态内容提供了一个解决方案。以下是你使用Self Sizing Cells时需要注意的事项:

1.cell用Auto Layout约束

2.指定表视图的estimatedRowHeight

3.将表视图的rowHeight属性设置为UITableViewAutomaticDimension

代码为

tableView.estimatedRowHeight = 44.0

tableView.rowHeight = UITableViewAutomaticDimension
参考链接为http://www.cocoachina.com/ios/20140922/9717.html
(3)estimatedSectionHeaderHeight,estimatedSectionFooterHeight同理
 (4)separatorInset--自定义分割线的inset
(5)backgroudview--在tableview中添加背景控件,这个控件不管宽高设定为多少,都默认为tableview的宽高
(6)返回表格中指定indexPath对应的cell
- ( UITableViewCell *)cellForRowAtIndexPath:( NSIndexPath *)indexPath;

(7)返回指定cell的indexPath

- ( NSIndexPath *)indexPathForCell:( UITableViewCell *)cell;

(8)返回表格中指定点所在的indexPath

- ( NSIndexPath *)indexPathForRowAtPoint:( CGPoint )point;

(9)返回表格中指定区域内所有indexPath 组成的数组

- ( NSArray *)indexPathsForRowsInRect:( CGRect )rect;   

(10)返回表格中所有可见区域内cell的数组

- ( NSArray *)visibleCells;

(11)返回表格中所有可见区域内cell对应indexPath所组成的数组

- ( NSArray *)indexPathsForVisibleRows;

(12)控制该表格滚动到指定indexPath对应的cell的顶端 中间 或者下方

- ( void )scrollToRowAtIndexPath:( NSIndexPath *)indexPath atScrollPosition:( UITableViewScrollPosition )scrollPosition animated:( BOOL )animated;

(13)控制该表格滚动到选中cell的顶端 中间 或者下方

-( void )scrollToNearestSelectedRowAtScrollPosition:( UITableViewScrollPosition )scrollPosition animated:( BOOL )animated;

(14)调用- (void)insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

- (void)deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;方法时,应该先修改- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section相应的数值

tableViewCell 属性

shouldIndentWhileEditing--当编辑时可以缩进

indentationWidth--缩进宽度

indentationLevel--缩进级别,使cell内容向右缩进

 cell.indentationWidth=0;
 cell.indentationLevel=0;

 cell.indentationWidth=2;
 cell.indentationLevel=10;

tableview实用小技巧---参考http://ios.jobbole.com/84377/

  
a:当我们的数据未能显示满一屏幕的时候,UITableView会显示多余的横线,这个时候你如果希望去掉这些横线,你可以加上这句话

 self.tableView.tableFooterView = [[UIView alloc]init];

b:UITableView的分割线默认是开头空15像素点的(好像是15来着~~),产品经理有时候希望能够定格显示,那么你可能会这么做。
 self.tableView.separatorInset = UIEdgeInsetsZero;

但是你很快就会发现这么做并没有效果,这是因为 separatorInset这个属性在iOS7以后就已经失效了,但是我们还是能够达到同样的效果,你可以在你的tablevView的代理协议实现界面加上下面这段代码:

-(void)viewDidLayoutSubviews
{
  if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
      [self.tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
  }
 
  if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
      [self.tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
  }
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
  if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
      [cell setSeparatorInset:UIEdgeInsetsZero];
  }
  if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
      [cell setLayoutMargins:UIEdgeInsetsZero];
  }
}


tableview在显示数据时,先执行


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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section——>

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath——>

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

如果想对tableview添加、删除、移动数据,则这样:

(1)仅删除:

a:左滑删除


 (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{return UITableViewCellEditingStyleDelete;}----》- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

    if(editingStyle==UITableViewCellEditingStyleDelete){

        [self.array removeObjectAtIndex:indexPath.row];

        [self.tablevi deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];

    }

}

b:编辑删除


<pre name="code" class="objc">- (IBAction)editClick:(id)sender {
    self.tablevi.editing?[self.tablevi setEditing:NO animated:YES]:[self.tablevi setEditing:YES animated:YES];
    
}//编辑按钮点击事件
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if(editingStyle==UITableViewCellEditingStyleDelete){
        [self.array removeObjectAtIndex:indexPath.row];
        [self.tablevi deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
}


 (2)仅添加 

- (IBAction)editClick:(id)sender {
    self.tablevi.editing?[self.tablevi setEditing:NO animated:YES]:[self.tablevi setEditing:YES animated:YES];
    
}//编辑按钮点击事件
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleInsert;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if(editingStyle==UITableViewCellEditingStyleInsert){
       [self.array insertObject:@"F" atIndex:indexPath.row+1];
        NSIndexPath *indexPath1=[NSIndexPath indexPathForItem:indexPath.row+1 inSection:0];
        [self.tablevi insertRowsAtIndexPaths:@[indexPath1] withRowAnimation:UITableViewRowAnimationLeft];

    }
}
(3)移动行


- (IBAction)editClick:(id)sender {
    self.tablevi.editing?[self.tablevi setEditing:NO animated:YES]:[self.tablevi setEditing:YES animated:YES];
    
}//编辑按钮点击事件
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleNone;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    [self.array exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
}
(4)删除和添加

//编辑按钮点击事件
- (IBAction)editClick:(id)sender {
    
    self.tablevi.editing?[self.tablevi setEditing:NO animated:YES]:[self.tablevi setEditing:YES animated:YES];
    self.tablevi.allowsMultipleSelectionDuringEditing=NO;
    self.tablevi.allowsMultipleSelection=NO;

    
}
//确定按钮点击事件
 - (IBAction)sureClick:(id)sender {
    NSArray <NSIndexPath *> *indexPaths=[self.tablevi indexPathsForSelectedRows];
    for(NSIndexPath *index in indexPaths){
        [self.array removeObjectAtIndex:index.row];
    }
    [self.tablevi beginUpdates];
    [self.tablevi deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];//需要执行的操作
    [self.tablevi endUpdates];
    
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert;
}


 



  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值